如何从R中的变量名列表中调用变量 [英] How to call variables from a list of variable names in R

查看:173
本文介绍了如何从R中的变量名列表中调用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量名列表。

list = c("smoke", "ptd", "ht", "ui", "racecat", "visit")

我要绘制一系列图

plot(low[somevariable=="0"] ~ age[somevariable=="0"])

我想用列表中的每个变量名替换一些变量。例如,

I'd like to replace somevariable with each of the variable name in the list. For example,

plot(low[smoke=="0"] ~ age[smoke=="0"])
plot(low[ptd=="0"] ~ age[ptd=="0"])
......
plot(low[visit=="0"] ~ age[visit=="0"])

我尝试创建一个for循环并使用各种方法替换某些变量,例如

I've tried to create a for-loop and use a variety of things to replace somevariable, such as

list[i], paste(list[i]), substitute(list[i]), parse(list[i])

但它们都不起作用。我非常感谢您的帮助。提前致谢。

But none of them works. I would really appreciate some help. Thanks in advance.

推荐答案

您可以尝试:

for(i in list) {
  print(plot(low[get(i) == 0] ~ low[get(i) == 0]))
}

这篇关于如何从R中的变量名列表中调用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆