0

This is probably easy to do, but I could not find a solution.

Say I have a serie of variables : variable1, variable2 ... I would like to use another variable/list like X<-c("variable1", variable2"...) to call one of the variable (variable1, variable2...) in a function.

Something like : plot(X[1], Y) where 'X[1]' is interpreted as 'variable1'.

Any suggestion ?

Thank you

Guillaume

----- Edit after the first anwsers --

Well the get() function was what I needed. I had tried it before, but could not get what I wanted. I does work now.

Thanks jlhoward and Ananda Mahto for your help.

There are some more explanations for future (potential) readers:

With myData, a dataframe with 3 columns (Value1, Value2, Y)

I can do :

plot(Value1~Y, data=myData)

or

plot(Value2~Y, data=myData)

now if I have a vector such as

myList<-c("Value1", "Value2")

I can do :

i=1
plot(get(myList[i])~Y, data=myData)

or

i=2
plot(get(myList[i])~Y, data=myData)

anymore comments / suggestions welcome

5
  • From your description it's not clear what you want. You probably should use a list. You might want get, but I would discourage you to use it. Commented Dec 6, 2013 at 12:55
  • Usually, you would use one of the apply family (like lapply or sapply). Can you provide a minimal example of what you want to do and what you've tried. Commented Dec 6, 2013 at 12:55
  • My apologies if this was not clear. Commented Dec 6, 2013 at 13:01
  • What I would like to do is to use a variable 'X' (or a list) as an argument in a function such as plot(X), where X does not contain the values I want to plot, a list a variables (the one that I want to plot). This could be use in a loop for example. Commented Dec 6, 2013 at 13:05
  • @AnandaMahto, it looks like something like that. I will test it right now. thanks Commented Dec 6, 2013 at 13:12

2 Answers 2

1

Here's a combination of my comment and Roland's. Hopefully, it helps get you closer to answering your question or helps you clarify what you're trying to do.

First, some variables, "A", "B", and "C"

set.seed(1)
A <- rnorm(100)
B <- rnorm(100)
C <- rnorm(100)

Next, a vector of the names of those variables, and another variable, "Y" that we want to plot against.

X <- c("A", "B", "C")
Y <- 1:100

Here's one approach:

lapply(X, function(x) plot(x = get(x), y = Y))

The preferred approach, though, especially if you're going to be doing similar things on your vectors, is to just put those vectors in a list first and work from there:

myList <- list(A, B, C)
lapply(myList, function(x) plot(x = x, y = Y))
# lapply(myList, sum) 
## or whatever else you want to do with each of these vectors
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you meant?

f = function(x){return(x*x)}
X= c(1,2,3)
sapply(X,f)
[1] 1 4 9

sapply "applies" the function f for each element in the list X, and produces a vector as output. You can also do:

sapply(1:3,function(x){return(x*x)})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.