0

I need to write a line of R that can reference an element of a list using a dynamically generated name.

For example, if I have the list:

x <- list(a = "Foo", b = "Bar", c = "Salad")

And a variable

v1 <- sample(c("a", "b", "c"),1)

I need to be able to change an element of that list using something like

x$v1 <- "Something else"

This last line, of course, does not work. Any help greatly appreciated.

1
  • use the x[[v1]] <- "Something else" instead of $ Commented Nov 19, 2016 at 12:01

1 Answer 1

0

This was answered in the comment above:

use the x[[v1]] <- "Something else" instead of $ – akrun

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.