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))
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
do.call("rbind", your.list)
Patrick Burns
Burns Statistics
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
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
>
>
many very helpful people wrote:>do.call("rbind", list)Thank you very much everybody. I wondered what the deal was WRT do.call(). Could we add this or a similarly educational example to the help(do.call) page? rksh
On Thu, 16 Oct 2003, Robin Hankin wrote:> > what I want is the equivalent of > > R> rbind(a[[1]],a[[2]],a[[3]],a[[4]],a[[5]]) >Note that while do.call("rbind",a) is the natural solution it may in fact be faster to create a matrix and use a loop to put things in it. rval<-matrix(ncol=4, nrow=sum(sapply(a,nrow))) i<-1 for(ai in a){ r<-nrow(ai) rval[i+1:r,]<-ai i<- i+r } -thomas