[R] turn list into dataframe

arun smartpink111 at yahoo.com
Tue Oct 9 00:26:40 CEST 2012


Hi,

Your output suggests that the list elements have unequal lengths.  The empty spaces will be occupied by NAs.  
I am using the first three list elements from the tapply() output:

list1<-list(c(2.8546, 4.0778,  5.2983,  6.3863,  7.5141,  8.5498,  9.5839, 10.6933),c(7.6810,  8.7648,  9.8382, 10.8903, 11.9840, 13.0541, 14.1132, 15.1657),c(22.0840, 30.3001, 35.2505, 42.8085, 48.5220, 52.0604, 57.9428, 61.4930,
 64.4550, 67.3543, 69.8435, 72.9508, 74.4730, 76.3104))
 names(list1)<-c("SNRL1Core120","SNRL1Core230","VAL1.1.1NA.na30")

 fun1<-function(x){
 na.pad<-function(y,len){
 c(y,rep(NA,len-length(y)))
 }
 maxlen<-max(sapply(x,length))
 do.call(data.frame,lapply(x,na.pad,len=maxlen))
 }
 fun1(list1)
A.K. 







----- Original Message -----
From: Benjamin Caldwell <btcaldwell at berkeley.edu>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Monday, October 8, 2012 5:49 PM
Subject: [R] turn list into dataframe

Dear R users,

I'm starting to use 'apply' functions rather than for loops in R, and
sometimes the output is a bit different than what I want. In this case, the
command was


tapply(myvector,myindex,cumsum)

And the output was something like this:

$`SNRL1 Core 120`
[1]  2.8546  4.0778  5.2983  6.3863  7.5141  8.5498  9.5839 10.6933

$`SNRL1 Core 230`
[1]  7.6810  8.7648  9.8382 10.8903 11.9840 13.0541 14.1132 15.1657

$`VAL 1.1.NA.na30`
[1] 22.0840 30.3001 35.2505 42.8085 48.5220 52.0604 57.9428 61.4930
64.4550 67.3543 69.8435 72.9508 74.4730 76.3104

$`VAL 1.2.NA.na15`
[1] 33.8895 38.7440 41.0536 44.1581 46.4891 48.3130 51.0973 52.9241
54.6404 56.1265 57.5064 59.0745

$`VAL 1.2.NA.na30`
[1]  6.6408 10.6838 13.8328 15.5435 18.3037 20.3315 22.8817 24.4481
26.4106 27.6658 29.6455 30.8490 31.8680

$`VAL 1.3.NA.na10`
[1]  4.8198  7.1274  8.9536 11.5954 14.0845 15.5116 16.9462 18.1269
19.3453 20.5723 21.7122 22.8643

$`VAL 1.3.NA.na20`
[1]  5.7382  8.2056  9.4489 10.8225 12.3497 13.6879 15.1077 16.3229

,
That's fine, but I need the output as a dataframe. I'm not even sure what
to call this list, but it has multiple entries for each item.

Forgive the fact that I don't have the data for you to use, I'm wondering
if anyone knows about a pre-existing function that will allow be to turn
the above list form into a dataframe. Thanks


*Ben Caldwell*

    [[alternative HTML version deleted]]

______________________________________________
R-help at 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.





More information about the R-help mailing list