I would like to insert an element into a list in R. The problem is that I would like it to have a name contained within a variable.
> list(c = 2)
$c
[1] 2
Makes sense. I obviously want a list item named 'c', containing 2.
> a <- "b"
> list(a = 1)
$a
[1] 1
Whoops. How do I tell R when i want it to treat a word as a variable, instead of as a name, when I am creating a list?
Some things I tried:
> list(eval(a)=2)
Error: unexpected '=' in "list(eval(a)="
> list(a, 2)
[[1]]
[1] "b"
[[2]]
[1] 2
> list(get(a) = 2)
Error: unexpected '=' in "list(get(a) ="
I know that if I already have a list() laying around, I could do this:
> ll<-list()
> ll[[a]]=456
> ll
$b
[1] 456
...But:
> list()[[a]]=789
Error in list()[[a]] = 789 : invalid (NULL) left side of assignment
How can I construct an anonymous list containing an element whose name is contained in a variable?