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)
.
Try using double brackets instead of single brackets:
tmp1 <- 'Sepal.Width'
tmp2 <- 'Species'
boxplot( iris[[tmp1]] ~ iris[[tmp2]] )
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))
您可以使用paste
来构造公式,然后转换为boxplot调用的公式:
boxplot(as.formula(paste(temp,"Var2",sep="~")),DF)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.