0

I have a set of strings

a = c("b1","b5","b7")

I have a list abc that has data frames b1 b2 b3 b4 b5 b6 b7 when I try

for(i in a) {
  cde$i = abc$as.name(i)
}

I get an error because abc$as.name(i) is incorrect. My question is How can I tell R to look at abc$"b1" as abc$b1

abhi
  • 53
  • 9

1 Answers1

1

To get an element of a list by the value of an other variable, use [[x]] not anything involving $ signs.

LIST = list(a=1, b=2, c=3)
a = "b"
LIST$a # gets 1, always the literal value after the dollar
LIST[[a]] # gets 2, evaluates its argument, results in string "b"  
LIST[["a"]] # gets 1, same as LIST$a, argument evaluates to a string.
Spacedman
  • 92,590
  • 12
  • 140
  • 224