I use lapply and sapply as my go-to functions. So far so good, but why the following code does not work puzzles me.
df<-as.data.frame(matrix(rnorm(50),ncol=5))
names(df)<-c("x1","x2","x3","x4","x5")
df1<-seq_len(10)
ll<-lapply(seq(1,5), function(i) qplot(df1,df[,i]))
I get an error message:
Error in `[.data.frame`(df, , i) : undefined columns selected
Well, apparently, I made a rather unfortunate mistake in my reproducible code. It works now, but all the graphs in the list llare the same plot. When I ran this:
do.call(grid.arrange,ll)
I get the following image:

All stories are the same! This is also the output that I get when I run it through my data.
source
share