Stephen, I am sure someone will have a more elegant solution, but the following works. Mark
d.lst <- split(x = d, f = as.factor(d$Group), drop = FALSE) d.lst.mn <- sapply(d.lst, FUN = function(x){mean(as.numeric(as.character(x$Value)))}) o <- order(d.lst.mn, decreasing = TRUE) d.lst.mn <- d.lst.mn[o] e <- NULL for (i in 1:length(d.lst.mn)){ if (i == 1){ e <- d[d$Group == names(d.lst.mn)[i],] } else { e <- rbind(e, d[as.character(d$Group) == names(d.lst.mn)[i],]) } } e Mark W. Kimpel MD ** Neuroinformatics ** Dept. of Psychiatry Indiana University School of Medicine 15032 Hunter Court, Westfield, IN 46074 (317) 490-5129 Work, & Mobile & VoiceMail (317) 204-4202 Home (no voice mail please) mwkimpel<at>gmail<dot>com ****************************************************************** sjbarry wrote: > Hi, > > I have what I think is a fairly straightforward problem. I've looked > through the FAQ's and mailing lists but have been unable to identify a > solution, probably because I don't understand the language well enough. > > I have a set of data d, with 3 columns as shown, > I want to sort the data Group, mean(Value by Label). I know that this > can be done for one level, say Label, using factor() but I cannot see > how to extend that. I have included the code to create the data.frame > below and would greatly appreciate a solution or a link to a similar > problem that has already been solved in the mailing list. > Value Label Group > 19 Big A > 29 Big A > 39 Big A > 55 Small D > 33 Small D > 11 Small D > 55 Small D > 66 Small D > 11 Small D > 2 Big C > 3 Big C > 3 Big C > 3 Big C > 3 Big C > 3 Big C > 3 Big C > 3 Small B > 2 Small B > 5 Small B > 6 Small B > 5 Small B > 6 Small B > > Value <- c(19,29,39,55,33,11,55,66,11,2,3,3,3,3,3,3,3,2,5,6,5,6) > Group <- c("A","A","A","D","D","D","D","D","D", > "C","C","C","C","C","C","C","B","B","B","B","B","B") > Label <- c("Big","Big","Big", > "Small","Small","Small","Small","Small","Small", > "Big","Big","Big","Big","Big","Big","Big", > "Small","Small","Small","Small","Small","Small") > d <- as.data.frame(cbind(Value, Label, Group)) > > > Thanks > > Stephen Barry > ______________________________________________ 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.