Skip to content
Back to formatted view

Raw Message

Message-ID: <788330.27674.qm@web32808.mail.mud.yahoo.com>
Date: 2008-03-13T18:53:18Z
From: John Kane
Subject: Splitting a set of vectors in a list  (Solved )
In-Reply-To: <da79af330803131129l3feff84r47f69f0101ec085c@mail.gmail.com>

My thanks to Henrique Dallazuanna and Phil Spector. 
Both solutions worked well. 
Phil suggested that an alterative to my function would
be 
vect1 = sapply(mylist,'[[',1)
and I see that Henrique used `[` in his solution.

Can you point me to some documentation that discusses
these usages. I have seen them before but I have never
actually figured out how to use them.? 

Thanks.

Problem and solutions
========================================================
mylist <- list(aa=c("cat","peach" ), bb=c("dog",
"apple", "iron"), 
         cc = c("rabbit", "orange", "zinc", "silk"))
myfun <- function(dff) dff[1]       
vect1  <- unlist(lapply(mylist, myfun))

# Desired output
t(cbind( c("cat" ,  "peach" , NA, NA), bbb  <- c("dog"
,  "apple" ,"iron", NA),
ccb <- c("rabbit" ,"orange" ,"zinc" ,  "silk" ))) 

# Phil Spector's approach
mlen = max(sapply(mylist,length))
eqlens = lapply(mylist,function(x)if(length(x) < mlen)
                           
c(x,rep('',mlen-length(x))) else x)
do.call(rbind,eqlens)

# 	"Henrique Dallazuanna" <wwwhsd at gmail.com>
#    I added the t()
t(as.data.frame(lapply(mylist, `[`,
1:max(unlist(lapply(mylist,
 length))))))


[[elided trailing spam]]