使用字符串创建变量并将其放入数据框中

时间:2015-12-30 18:43:58

标签: r

我正在尝试使用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=" =/= "))

0 个答案:

没有答案