Home > database >  Indexing: How do I reassign multiple objects at once?
Indexing: How do I reassign multiple objects at once?

Time:08-26

a <- c("red", "yellow", "blue") a[1] <- "green"

a [1] "green" "yellow" "blue"

If I want to change 2 at the same time, how do I do this? e.g. a[1][2] <- "purple" "grey"

or if I had a longer list, and I wanted to change entries 1:5 to grey, how would I do this?

CodePudding user response:

You should use c to concatenate objects to create a vector. Careful: you must respect the order.

> a[c(1, 2)] <- c("purple", "grey")
> a
[1] "purple" "grey"   "blue"  
  •  Tags:  
  • r
  • Related