简体   繁体   中英

For Loop - Box plots

In R, how do I create boxplots of specific columns with a for loop?

So far I have

for(colnames in c("Delivery, Free_wifi, Service, Alcohol")) {
    boxplot(df$Price ~ colnames) }

Using eval and as.name()

df=data.frame(Price=c(rnorm(4,1,1),rnorm(5,2,1.5)),Delivery=c(rep('A',4),rep('B',5)),`Another Column`=c(rep('aa',3),rep('bb',3),rep('cc',3)),check.names = FALSE)
for(colnames in c("Delivery","Another Column")) {
  boxplot(Price ~ eval(as.name(colnames)),data=df) #eval
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM