HI, Not sure I understand it correctly. dat1<- read.table(text=" site Year doy fish Feed swim agr_1 agr_2 agr_3 rest hide 3 2012 203 1 1 0 0 0 0 0 0 3 2012 203 1 0 1 0 0 0 0 0 3 2012 203 1 0 1 0 0 0 0 0 3 2012 203 2 0 0 0 0 0 1 0 3 2012 203 2 1 0 0 0 0 0 0 3 2012 203 2 1 0 0 0 0 0 0 4 2012 197 1 0 0 0 0 0 1 0 4 2012 197 1 1 0 0 0 0 0 0 4 2012 197 1 0 1 0 0 0 0 0 4 2012 197 3 0 0 0 0 0 0 1 4 2012 197 3 1 0 0 0 0 0 0 ",sep="",header=TRUE) dat2<-reshape(dat1,direction="long",varying=7:9,sep="_") row.names(dat2)<- 1:nrow(dat2) head(dat2) # site Year doy fish Feed swim rest hide time agr id #1 3 2012 203 1 1 0 0 0 1 0 1 #2 3 2012 203 1 0 1 0 0 1 0 2 #3 3 2012 203 1 0 1 0 0 1 0 3 #4 3 2012 203 2 0 0 1 0 1 0 4 #5 3 2012 203 2 1 0 0 0 1 0 5 #6 3 2012 203 2 1 0 0 0 1 0 6
library(plyr) #fish, year, site ddply(dat2,.(fish,Year,site),function(x) numcolwise(mean)(x[,c(5:8)])) # fish Year site Feed swim rest hide #1 1 2012 3 0.3333333 0.6666667 0.0000000 0.0 #2 1 2012 4 0.3333333 0.3333333 0.3333333 0.0 #3 2 2012 3 0.6666667 0.0000000 0.3333333 0.0 #4 3 2012 4 0.5000000 0.0000000 0.0000000 0.5 #fish ddply(dat2,.(fish),function(x) numcolwise(mean)(x[,c(5:8)])) # fish Feed swim rest hide #1 1 0.3333333 0.5 0.1666667 0.0 #2 2 0.6666667 0.0 0.3333333 0.0 #3 3 0.5000000 0.0 0.0000000 0.5 A.K. >Hi >I did fish behavior at different sites. >Each fish represent a rep at each site. >e.g for my data >site Year doy fish Feed swim agr_1 agr_2 agr_3 rest >hide >3 2012 203 1 1 0 0 0 0 0 >0 >3 2012 203 1 0 1 0 0 0 0 >0 >3 2012 203 1 0 1 0 0 0 0 >0 >3 2012 203 2 0 0 0 0 0 1 >0 >3 2012 203 2 1 0 0 0 0 0 >0 >3 2012 203 2 1 0 0 0 0 0 >0 >4 2012 197 1 0 0 0 0 0 1 >0 >4 2012 197 1 1 0 0 0 0 0 >0 >4 2012 197 1 0 1 0 0 0 0 >0 >4 2012 197 3 0 0 0 0 0 0 >1 >4 2012 197 3 1 0 0 0 0 0 >0 > >1. I would like to combine column agr_1, agr_2 and agr_3 >2. How to calculate mean for each fish for each behavior >Any suggestion is appreciated. Thanks ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.