[R] Sorting list elements according to their mean value
mtb954 at gmail.com
mtb954 at gmail.com
Tue Mar 3 20:01:14 CET 2015
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]]
More information about the R-help
mailing list