Carlos Hernandez
2009-Sep-03 14:16 UTC
[R] extract and replace columns of matrices stored in a list
Dear All, I created a list (of length Z) in the following way: my.array <- vector("list", Z) then i assigned a matrix (of T rows by N columns) in each of the elements of the list my.array in the following way: my.array[[i]] <- matrix.data ##( matrix.data has dimensions TxN, and i repeated this command for i from 1 to Z, the matrix.data contains only numeric data) and 1. i would like to extract all the third columns of each of the Z matrices stored in my.array (such that i get a new list only with the 3rd columns of each matrix in the elements of a new list) 2. i would like to know how could i replace all the 3rd columns of each matrix in my.array if i have a second matrix (size ZxT) with these columns. is there a simple way to do these tasks? i appreciate any hints or advice. Carlos [[alternative HTML version deleted]]
Henrique Dallazuanna
2009-Sep-03 14:34 UTC
[R] extract and replace columns of matrices stored in a list
Try this: #1 lapply(my.array, '[', , 3) #2 newThirdColumn <- sample(3) lapply(my.array, replace, list = 7:9, values = newThirdColumn) On Thu, Sep 3, 2009 at 11:16 AM, Carlos Hernandez <carlos.uni2@gmail.com>wrote:> Dear All, > I created a list (of length Z) in the following way: > > my.array <- vector("list", Z) > > then i assigned a matrix (of T rows by N columns) in each of the elements > of > the list my.array in the following way: > > my.array[[i]] <- matrix.data ##( matrix.data has dimensions TxN, and i > repeated this command for i from 1 to Z, the matrix.data contains only > numeric data) > > and > 1. i would like to extract all the third columns of each of the Z matrices > stored in my.array (such that i get a new list only with the 3rd columns of > each matrix in the elements of a new list) > > 2. i would like to know how could i replace all the 3rd columns of each > matrix in my.array if i have a second matrix (size ZxT) with these columns. > > is there a simple way to do these tasks? i appreciate any hints or advice. > > Carlos > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]