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.

cpokane
  • 23
  • 5

1 Answers1

0

This was answered in the comment above:

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

cpokane
  • 23
  • 5