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

Liaw, Andy andy_liaw at merck.com
Fri Mar 14 17:26:55 CET 2008


From: Bert Gunter
 
> ?"["  ?InternalMethods
> 
> x[i,j] is just shorthand for  "["(x,i,j) . (AFAIK)**All** operators
> (+,-,...,subscripting,...) in R are functions, stemming from 
> its LISP-like
> heritage, and can actually called by the usual functional 
> syntax, f(...),
> instead of the operator syntax.

That is true even for assignment:

R> "<-"(junk, 1:3)
R> junk
[1] 1 2 3

and "{":

R> "{"(1, 2, 3)
[1] 3


I believe this is in the (draft) R Language Definition, part of the
official manuals that shipped with R.

Andy
 
> Not sure where this is explicitly discussed within R's 
> documentation, but
> you can find info on it in V&R's "S Programming", esp. p.24 and 4.3,
> "Extracting or replacing coefficients".
> 
> No doubt, other S/R  books explain it also.
> 
> Cheers,
> 
> Bert Gunter
> Genentech Nonclinical Statistics
> 47374
> 
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On
> Behalf Of John Kane
> Sent: Thursday, March 13, 2008 11:53 AM
> To: Henrique Dallazuanna
> Cc: R R-help
> Subject: Re: [R] Splitting a set of vectors in a list (Solved )
> 
> 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]]
> 
> ______________________________________________
> 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.
> 
> ______________________________________________
> 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.
> 
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachme...{{dropped:15}}



More information about the R-help mailing list