Search code examples
rvariablescall

How to call variables from a list of variable names in R


I have of list of variable names.

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

I want to make a series of plots like this.

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"])

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.


Solution

  • You can try:

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