[R] Write columns from within a list to a matrix?
Hello, I think I have a mental block when it comes to working with lists. lapply and sapply appear to do some magical things, but I can't seem to master their usage. As an example, I would like to convert a column within a list to a matrix, with the list element corresponding to the new matrix column. #Here is a simplified example: . test=vector(list, 3) for (i in 1:3){ test[[i]]=cbind(runif(15), rnorm(15,2)) } #create example list (I'm sure there is a better way to do this too). #Now, I wan to get the second column back out, converting it from a list to a matrix. This works, but gets confusing/inefficient when I have multiple complex lists I am trying to manage. savecol2=matrix(0,15,0) for (i in 1:3){ savecol2=cbind(savecol2, test[[i]][,1]) } #Something like??: (of course this doesn't work) savecol2=sapply(test, [[, function(x) x[2,]) Thank you! Jeff __ R-help@stat.math.ethz.ch 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.
Re: [R] Write columns from within a list to a matrix?
test - lapply(1:3, function(i) cbind(runif(15), rnorm(15,2))) sapply(test, [, 16:30) b On Jul 22, 2007, at 3:39 PM, [EMAIL PROTECTED] wrote: Hello, I think I have a mental block when it comes to working with lists. lapply and sapply appear to do some magical things, but I can't seem to master their usage. As an example, I would like to convert a column within a list to a matrix, with the list element corresponding to the new matrix column. #Here is a simplified example: . test=vector(list, 3) for (i in 1:3){ test[[i]]=cbind(runif(15), rnorm(15,2)) } #create example list (I'm sure there is a better way to do this too). #Now, I wan to get the second column back out, converting it from a list to a matrix. This works, but gets confusing/inefficient when I have multiple complex lists I am trying to manage. savecol2=matrix(0,15,0) for (i in 1:3){ savecol2=cbind(savecol2, test[[i]][,1]) } #Something like??: (of course this doesn't work) savecol2=sapply(test, [[, function(x) x[2,]) Thank you! Jeff __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] Write columns from within a list to a matrix?
oh! and if you want to be less ad-hoc: sapply(test, function(x) x[,2]) b On Jul 22, 2007, at 3:50 PM, Benilton Carvalho wrote: test - lapply(1:3, function(i) cbind(runif(15), rnorm(15,2))) sapply(test, [, 16:30) b On Jul 22, 2007, at 3:39 PM, [EMAIL PROTECTED] wrote: Hello, I think I have a mental block when it comes to working with lists. lapply and sapply appear to do some magical things, but I can't seem to master their usage. As an example, I would like to convert a column within a list to a matrix, with the list element corresponding to the new matrix column. #Here is a simplified example: . test=vector(list, 3) for (i in 1:3){ test[[i]]=cbind(runif(15), rnorm(15,2)) } #create example list (I'm sure there is a better way to do this too). #Now, I wan to get the second column back out, converting it from a list to a matrix. This works, but gets confusing/inefficient when I have multiple complex lists I am trying to manage. savecol2=matrix(0,15,0) for (i in 1:3){ savecol2=cbind(savecol2, test[[i]][,1]) } #Something like??: (of course this doesn't work) savecol2=sapply(test, [[, function(x) x[2,]) Thank you! Jeff __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] Write columns from within a list to a matrix?
Very close... Actually it's more like savecol2=sapply(test, function(x) x[,1]) to get the same matrix as you showed in your for-loop (did you actually want the first or second column?). when I have multiple complex lists I am trying to manage... for this, you can try mapply() which goes something like mapply(function(x,y) #...function body...#, x=list1,y=list2) --- [EMAIL PROTECTED] wrote: Hello, I think I have a mental block when it comes to working with lists. lapply and sapply appear to do some magical things, but I can't seem to master their usage. As an example, I would like to convert a column within a list to a matrix, with the list element corresponding to the new matrix column. #Here is a simplified example: . test=vector(list, 3) for (i in 1:3){ test[[i]]=cbind(runif(15), rnorm(15,2)) } #create example list (I'm sure there is a better way to do this too). #Now, I wan to get the second column back out, converting it from a list to a matrix. This works, but gets confusing/inefficient when I have multiple complex lists I am trying to manage. savecol2=matrix(0,15,0) for (i in 1:3){ savecol2=cbind(savecol2, test[[i]][,1]) } #Something like??: (of course this doesn't work) savecol2=sapply(test, [[, function(x) x[2,]) Thank you! Jeff __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.