Suppose I have an object which is a list and each element of the list is
a matrix (this list comes as a result of using lapply). Each of the
matrices has the same number of columns and I would like to 'stack'
these matrices using rbind. One obvious way is with a loop and rbind:
>Quig.temp <- lapply(1:100, data.Quig1.sz)
>Quig1 <- Quig.temp[[1]]
>for(i in 2:100) Quig1 <- rbind(Quig1, Quig.temp[[i]])
The first line creates the list with 100 matrices and the next two lines
rbind the matrices together into a matrix called Quig1. Unfortunately,
these matrices are big (say on average 100 rows by 150 columns each) and
when I run the loop in the third line this runs for a long time. When I
tell you that I have to do this seven more times (101:800) you see that
I can't use my computer for a few days ;-)
I have looked at the help files and they suggest creating a matrix that
will be the size of the completed matrix and then to fill it in with the
matrices from the list. (Actually, the help file example does this row
by row). However, the only way I could think to do this was with two
loops as in:
> rbind.hou
function()
{
rows <- row.hou()
cols <- ncol(Quig.temp[[1]])
Quig <- matrix(0, rows, cols)
start <- 0
for(i in 1:100) {
for(j in 1:nrow(Quig.temp[[i]])) {
Quig[start + j, ] <- Quig.temp[[ i]][j, ]
}
start <- start + nrow(Quig.temp[[i]])
}
Quig
}
I should mention that each matrix has a different number of rows, which
is why I run the loop from 1:nrow(...). This seems to take as long, if
not longer.
What is the best (fastest?) way to 'rbind' these matrices together?
Your help is greatly appreciated!
Thanks.
Ken Brown
ken.brown@uni.edu
--
Ken Brown
University of Northern Iowa
Department of Economics
Cedar Falls, IA 50614-0129
"Give a man a fish, feed him for a day;
Teach a man to fish, feed him for a lifetime."
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|