mtb954 at gmail.com
2015-Mar-03 19:01 UTC
[R] Sorting list elements according to their mean value
Hello R-helpers, I have a list of 999 dataframes and I would like to sort the list by the mean value of one of the columns in the data frames. Here is a small, self-contained example: #begin example iterations<-999 d<-list() #resampled data f<-list() #fitted values r<-list() #residuals l<-list() for (i in 1:iterations){ iboot<-sample(1:nrow(cars),replace=TRUE) bootdata<-cars[iboot,] d[[i]]<-bootdata f[[i]]<-fitted(lm(bootdata$dist~bootdata$speed)) r[[i]]<-resid(lm(bootdata$dist~bootdata$speed)) t<-data.frame(d[i],f[i],r[i]);names(t)<-c("speed","dist","fitted","resid") l[[i]]<-t } #end loop #end example Now, I would like to sort the 999 elements in this list by the mean value of the column named "fitted". In other words, the list element with the smallest mean value of "fitted" would be the new first list element, the list element with the second smallest mean value of "fitted" would be second new list element, etc....up to the list element with the largest mean value of "fitted". Thank you for any help you can provide! Best wishes, Mark [[alternative HTML version deleted]]
William Dunlap
2015-Mar-03 19:12 UTC
[R] Sorting list elements according to their mean value
Use order(), as in sortListByMean <- function(List) { List[order(vapply(List, mean, 0))] } sortedL <- sortListByMean(l) Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Mar 3, 2015 at 11:01 AM, <mtb954 at gmail.com> wrote:> Hello R-helpers, > > I have a list of 999 dataframes and I would like to sort the list by the > mean value of one of the columns in the data frames. > > Here is a small, self-contained example: > > > #begin example > > iterations<-999 > > d<-list() #resampled data > > f<-list() #fitted values > > r<-list() #residuals > > l<-list() > > for (i in 1:iterations){ > > iboot<-sample(1:nrow(cars),replace=TRUE) > > bootdata<-cars[iboot,] > > d[[i]]<-bootdata > > f[[i]]<-fitted(lm(bootdata$dist~bootdata$speed)) > > r[[i]]<-resid(lm(bootdata$dist~bootdata$speed)) > > t<-data.frame(d[i],f[i],r[i]);names(t)<-c("speed","dist","fitted","resid") > > l[[i]]<-t > > } #end loop > > #end example > > > Now, I would like to sort the 999 elements in this list by the mean value > of the column named "fitted". > > > In other words, the list element with the smallest mean value of "fitted" > would be the new first list element, the list element with the second > smallest mean value of "fitted" would be second new list element, etc....up > to the list element with the largest mean value of "fitted". > > > Thank you for any help you can provide! > > > Best wishes, Mark > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]