2

How can I access a column by using a variable that contains the name of the column?

Let's assume we have a data frame DF with 3 columns: Var1 Var2 Var3, where Var3 contains numerical data and Var1 as well as Var2 contain a few factors.

We would like to produce 2 boxplots using a temporary variable that contains the name of the column:

temp<-"Var3"
boxplot(DF[temp]) #(<--that works).

If I use the same method to obtain a boxplot for each factor in Var2, it doesn't:

boxplot(DF[temp]~DF$Var2) #(<-- does not work).

How can I get this working?

Annotation: If I use the name "Var3" directly, it does work and shows several boxplots:

boxplot(DF$Var3~DF$Var2).

3 Answers 3

7

Try using double brackets instead of single brackets:

tmp1 <- 'Sepal.Width'
tmp2 <- 'Species'
boxplot( iris[[tmp1]] ~ iris[[tmp2]] )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this suggestion. Before I tried [,[tmp1]] which did not work. Your solution is even easier to carry out and it works nicely.
1

You could simply do this. The with statement instructs boxplot to look for variables inside DF, the get statement accesses the object with name tmp.

with(DF, boxplot(get(tmp) ~ Var2))

Here is an illustrative example

tmp <- 'wt'
with(mtcars, boxplot(get(tmp) ~ cyl))

enter image description here

1 Comment

Dear Ramnath, thanks a lot for your answer! I especially liked that you give a detailed description on what the different elements in your code are doing.
1

You can use paste to construct the formula, and then convert to a formula for the boxplot call:

boxplot(as.formula(paste(temp,"Var2",sep="~")),DF)

1 Comment

It seems there are many different options to perform the task. I tried your solution and it works fine. Very interesting method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.