【发布时间】:2015-01-27 21:29:43
【问题描述】:
我能够得到字符变量 b 指向的列的内容。但我想将列 emp 设为 0。我该怎么做?
emp=c(1,2,30)
abc=data.frame(emp)
b="emp"
#below line gives content of column emp
eval(parse(text=paste("abc$", b, sep = "")))
#how can i replace each value in column emp with 0?
#below line doesnt work :(. It runs without error but values dont change
assign((text=paste("abc$", b, sep = "")),0)
abc
【问题讨论】:
-
你可以用
abc$emp替换eval(parse(text=paste("abc$", b, sep = ""))) -
我猜,你做这么复杂的事情是有特殊原因的?
-
我不能做 abc$emp 因为我不会知道列名。列名会一直变化,会保存在变量b中