Home > other >  How can I change an item in a vector?
How can I change an item in a vector?

Time:02-01

a<- vector(length = 3)

This line creates a vector a = [FALSE, FALSE, FALSE]. I want to change the first entry to TRUE.

a[0] = TRUE

Now, a returns [FALSE, FALSE, FALSE] without updating a[0], contradicting what is written on the following: https://www.educative.io/answers/how-to-change-the-value-of-a-vector-item-in-r. Is vector in R an immutable object?

CodePudding user response:

R counts from 1, unlike Python. Therefore, your implementation needs a minimal change for the second part

a <- vector(length = 3)
a[1] <- TRUE
a

I hope it helps. If it does, please tick this answer.

  •  Tags:  
  • r
  • Related