[R] lapply and boxplots with variable names

Shawn Morrison shawn.morrison at dryasresearch.com
Tue Jun 22 18:45:13 CEST 2010


Hi all,

I have a dataset with several variables, each of which is a separate 
column. For each variable, I want to produce a boxplot and include the 
name of the variable (ie, column name) on each plot.

I have included a sample dataset below. Can someone tell me where I am 
going wrong?

Thank you for your help,
Shawn Morrison

# Generate a sample dataset
var1 = rnorm(1000)
var2 = rnorm(1000)
TimePeriod = rep((LETTERS[1:4]), 250)

my.data = as.data.frame(cbind(var1, var2, TimePeriod)); summary(my.data)
attach(my.data)

# Create box plots for var1 and var2 using TimePeriod on the x-axis
lapply(my.data[,1:2], function(y) {
     boxplot(y~TimePeriod,
                 main = y
                 data = my.data)
     })



More information about the R-help mailing list