[R] Splitting a set of vectors in a list (Solved )
John Kane
jrkrideau at yahoo.ca
Thu Mar 13 20:20:16 CET 2008
Blood ell ! Thanks very much, it does help give a
feel for what's happening. I'll have to do some
experimenting.
--- Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> A example:
>
> x <- rnorm(5)
>
> x[3]
> `[`(x, 3)
> `[[`(x, 3)
>
> x[3:4]
> `[`(x, 3:4)
> `[[`(x, 3:4) # Error
>
>
> On 13/03/2008, John Kane <jrkrideau at yahoo.ca> wrote:
> > Ah ?Extract. Thanks
> >
> > Unless I'm missing something there is nothing in
> the
> > Usage or Examples to suggest to a naive reader
> like me
> > that one can use an unbalanced [ or [[, that is
> > withoug a corresponding ] or ]].
> >
> > I probably am just not understanding the details.
> >
> >
> > --- Henrique Dallazuanna <wwwhsd at gmail.com>
> wrote:
> >
> > > 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
> > >
> >
> >
> >
> >
> >
> ____________________________________________________
> the web, and bookmark your favourite sites. Download
> it now at
> > http://ca.toolbar.yahoo.com.
> >
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paran�-Brasil
> 25� 25' 40" S 49� 16' 22" O
>
[[elided trailing spam]]
More information about the R-help
mailing list