Marion Wenty
2011-Oct-11 07:47 UTC
[R] binding all elements of list (character vectors) to a matrix as rows
dear r-users, i have got a problem which i am trying to solve: i have got the following commands: Mymatrix <- matrix(1:9,ncol=3) Z <- list("V1"=c("a","",""),"V2"=c("b","",""),"V3"=c("c","",""),"V4"=c("d","","")) Mymatrix <- rbind(Mymatrix,Z[[1]],Z[[2]],Z[[3]],Z[[4]]) now this is working, but i would like to substitute Z[[1]],Z[[2]],Z[[3]],Z[[4]] for a command with which i could also use another list with a different number of elements, e.g. 5 or 6 elements. does anyone know the solution to this problem? thank you very much in advance! marion [[alternative HTML version deleted]]
Gerrit Eichner
2011-Oct-11 08:09 UTC
[R] binding all elements of list (character vectors) to a matrixasrows
Marion, try rbind( Mymatrix, do.call( "rbind", Z)) Hth -- Gerrit On Tue, 11 Oct 2011, Marion Wenty wrote:> dear r-users, > > i have got a problem which i am trying to solve: > > i have got the following commands: > > Mymatrix <- matrix(1:9,ncol=3) > Z <- > list("V1"=c("a","",""),"V2"=c("b","",""),"V3"=c("c","",""),"V4"=c("d","","")) > Mymatrix <- rbind(Mymatrix,Z[[1]],Z[[2]],Z[[3]],Z[[4]]) > > now this is working, but i would like to substitute > > Z[[1]],Z[[2]],Z[[3]],Z[[4]] > > for a command with which i could also use another list with a different > number of elements, e.g. 5 or 6 elements. > > does anyone know the solution to this problem? > thank you very much in advance! > > marion > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
David Winsemius
2011-Oct-11 08:31 UTC
[R] binding all elements of list (character vectors) to a matrix as rows
On Oct 11, 2011, at 2:47 AM, Marion Wenty wrote:> dear r-users, > > i have got a problem which i am trying to solve: > > i have got the following commands: > > Mymatrix <- matrix(1:9,ncol=3) > Z <- > list > ("V1 > "=c("a","",""),"V2"=c("b","",""),"V3"=c("c","",""),"V4"=c("d","",""))rbind(Mymatrix, t(as.data.frame(Z))) The next method could be used if you had more lists: do.call(rbind, list(Mymatrix, t(as.data.frame(Z))))> Mymatrix <- rbind(Mymatrix,Z[[1]],Z[[2]],Z[[3]],Z[[4]]) > > now this is working, but i would like to substitute > > Z[[1]],Z[[2]],Z[[3]],Z[[4]] > > for a command with which i could also use another list with a > different > number of elements, e.g. 5 or 6 elements.-- David Winsemius