Hi, Try this: myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type= c('x','x','y','z'))
z<-aggregate(length~Name,myData,mean) z1<-aggregate(length~type,myData,mean) merge(z,merge(z,z1),all=TRUE) Name length type 1 a 1.5 x 2 b 3.5 <NA> A.K. ----- Original Message ----- From: Filoche <pmassico...@hotmail.com> To: r-help@r-project.org Cc: Sent: Tuesday, July 3, 2012 12:04 PM Subject: [R] Data manipulation with aggregate Hi everyone. I have these data : myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type = c('x','x','y','z')) which gives me: Name length type 1 a 1 x 2 a 2 x 3 b 3 y 4 b 4 z I would group (mean) this DF using 'Name' as grouping factor. However, I have a field ('type') which is a string. I would like to use the unique value of this field when possible (i.e. when all the 'type' values are the same for each group) or replace with NA when 'type' has multiple values. In fact, I would like to obtain this: Name length type 1 a 1.5 x 2 b 3.5 NA For instance, I was using this command: aggregate(list(myData$length, myData$type), list(myData$Name), FUN = mean) But it can't deal with string data. I hope I have been clear enough. With regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/Data-manipulation-with-aggregate-tp4635298.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. ______________________________________________ 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.