Re: [R] Splitting a set of vectors in a list (Solved )

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Thu, 13 Mar 2008 15:58:54 -0300

On 13/03/2008, John Kane <jrkrideau_at_yahoo.ca> wrote:
> 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.?

See ?Extract

>
> 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))))))
>
>
> Looking for the perfect gift? Give the gift of Flickr!
>
> http://www.flickr.com/gift/
>
>

-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
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.
Received on Thu 13 Mar 2008 - 19:05:58 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 13 Mar 2008 - 19:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive