Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Indexing: How do I reassign multiple objects at once?

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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"  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading