I have a set of data that looks like this,
species<-"ABC" ind<-rep(1:4,each=24) hour<-rep(seq(0,23,by=1),4) depth<-runif(length(ind),1,50) df<-data.frame(cbind(species,ind,hour,depth)) df$depth<-as.numeric(df$depth)
In this example, the column "ind" has more levels and they don't have always the same length (here each individual has 4 levels, but in reality some individuals have thousands of rows of data, while other only a few lines).
What I would like to do is to have an outer loop or function that will select all the rows from each individual ("ind") and generate a boxplot using the depth/hour columns.
This is the idea that I have in mind,
for (i in 1:length(unique(df$ind))){ data<-df[df$ind==df$ind[i],] individual[i]<-data plot.boxplot<-function(data){ boxplot(depth~hour,dat=data,xlab="Hour of day",ylab="Depth (m)") } } par(mfrow=c(2,2),mar=c(5,4,3,1)) plot.boxplot(individual)
I realized that this loop might be inappropriate, but I am still learning. I can do the boxplot for each individual at a time, but I would like a faster, more efficient way of selecting the data for each individual and creating or storing boxplot results. This will be very useful for when I have many more individuals (instead of doing one at a time...). Thanks a lot in advance.