从R中的一组字符串中访问数据帧



我有一组字符串

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

我有一个具有数据帧b1 b2 b3 b4 b5 b6 b7的列表abc当我尝试

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

我会出错,因为abc$as.name(i)不正确。我的问题是我如何告诉r将ABC $" B1"视为ABC $ b1

要按其他变量的值获取列表的元素,请使用 [[x]],而不是任何涉及 $符号的东西。

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.

最新更新