[R] apply() question

Ben Bolker bolker at zoo.ufl.edu
Thu Oct 16 15:13:02 CEST 2003


do.call("rbind",list)

On Thu, 16 Oct 2003, Robin Hankin wrote:

> Hi
> 
> I have a function that returns an array with four columns but the
> number of rows differs with the calling argument.  I want to use
> something like sapply() to rbind() the outputs together.
> 
> Following toy example illustrates my problem:
> 
> f  <- function(i) {
>    options(warn= -1)
>    r <- ceiling(sqrt(i))
>    return(matrix(1:3,r,4))
> }
> 
> Thus sapply(1:5,f) is a list with five elements.
> 
> R> (a <- sapply(1:5,f))
> [[1]]
>       [,1] [,2] [,3] [,4]
> [1,]    1    2    3    1
> 
> #{...DELETED...}
> 
> [[5]]
>       [,1] [,2] [,3] [,4]
> [1,]    1    1    1    1
> [2,]    2    2    2    2
> [3,]    3    3    3    3
> 
> R>
> 
> what I want is the equivalent of
> 
> R> rbind(a[[1]],a[[2]],a[[3]],a[[4]],a[[5]])
> 
> but with an arbitrary upper limit and without any loops.  Anyone?
> 
> 
> Obligatory attempt:
> R> string<-paste("jj<-rbind(a[[1]] ",paste(",a[[",2:5,"]]",collapse=" "),")")
> R> eval(parse(text=string))
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
620B Bartram Hall                            bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704




More information about the R-help mailing list