我正在尝试使用for循环动态创建并为特定数据框的变量赋值。我想从一个字符串数组创建对象,在末尾添加单词“Check”,然后继续分配它。问题是,我无法将变量称为对象的元素。
可能比解释更容易展示......
diffs$derp = c("this", "that", "the other")
diffs$herp = c("esa", "esto", "el otro")
for(i in ncol(derp)) {
temp = paste0(derp[i], "Check")
diffs$temp = ifelse(derp[i] == derp[i], "Equal", paste(derp[i], herp[i], sep=" =/= "))
}
问题当然是这段代码认为我指的是diffs
中名为temp
的变量。我已经看到了assign()
函数,但我没有看到将新变量添加到数据框中的方法。
编辑: 我希望获得如下输出:
diffs$thisCheck = ifelse(diffs$this == diffs$that, "Equal",
paste(diffs$this, diffs$that, sep=" =/= "))