[R] list to data frame
arun
smartpink111 at yahoo.com
Wed Aug 28 19:07:22 CEST 2013
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_81 at yahoo.com>
To: "smartpink111 at yahoo.com" <smartpink111 at 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
More information about the R-help
mailing list