Hi,
set.seed(249)
dat1<- as.data.frame(matrix(sample(1:20,40*20,replace=TRUE),ncol=20))

#Based on your code:
 vecNew<-sort(t(as.data.frame(lapply(dat1,sum))),decreasing=TRUE)[1:10]

#or
vec1<-sort(unlist(lapply(dat1,sum),use.names=FALSE),decreasing=TRUE)[1:10]
 identical(vec1,vecNew)
#[1] TRUE


#Instead of using the ?lapply(),
vec2<-setNames(sort(colSums(dat1),decreasing=TRUE)[1:10],NULL)
 identical(as.numeric(vec1),vec2)
#[1] TRUE
A.K.





________________________________
From: farnoosh sheikhi <farnoosh...@yahoo.com>
To: "smartpink...@yahoo.com" <smartpink...@yahoo.com> 
Sent: Wednesday, August 28, 2013 12:35 PM
Subject: list to data frame



Hi there,

I have a data set and I want to get the sum of each column. Then order the 
result from high to low and draw a histogram for the top 10.

I was trying to use lapply(data, sum), but the result is in a list and even 
after changing to data frame and get the transpose, I can't get the right data 
format.

test<-as.data.frame(lapply(data, sum))
test<-t(test)

Thanks for your help.

Best,Farnoosh Sheikhi 

______________________________________________
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.

Reply via email to