引用data.frame的元素时出现未定义的列错误

时间:2019-01-17 23:35:33

标签: r dataframe

我正在尝试绘制许多图形,并且在引用data.frame的元素时遇到错误。

而不是手动更改变量名,我想遍历并引用特定的变量名。

执行此操作时,出现“未定义的选定列”错误。

运行此代码时,我得到了正确的图:

xy <- lm(Unfairness_Scale ~ OS_ImpCoreV_A * ImpCoreV_A, data = 
branch_annual)
with(branch_annual, interact_plot(xy, pred = OS_ImpCoreV_A, modx = 
ImpCoreV_A))

运行此代码时,出现“选择的未定义列”错误:

xy <- lm(branch_annual$Unfairness_Scale ~ branch_annual$OS_ImpCoreV_A * 
branch_annual$ImpCoreV_A, data = branch_annual)
with(branch_annual, interact_plot(xy, pred = branch_annual$OS_ImpCoreV_A, 
modx = branch_annual$ImpCoreV_A)) 

我尝试了几种不同的方法来引用数据帧的元素,但是我一直遇到相同的错误。我没有正确理解什么?

谢谢

塞巴斯蒂安

1 个答案:

答案 0 :(得分:0)

您可以将as.formula与字符一起用作循环输入,并在lm函数中构造公式。

xy <- lm(as.formula(paste('Unfairness_Scale', '~', 'OS_ImpCoreV_A', '* 
', 'ImpCoreV_A')), data = branch_annual)
with(branch_annual, interact_plot(xy, pred = 'OS_ImpCoreV_A', 
modx = 'ImpCoreV_A'))