Hi all. I am currently attempting to build a list of sparse matrixes. That I have already achieved, by> vmat <- list() > for (i in 1:n) { > vmat <- c(vmat, sparseMatrix(i,j,x=data) > }How I am trying to select those elements from the list where the column e.g. 999 is not null. I can do this for one of the sparse matrices with> which(vmat[[1]][,999] != 0)which returns the rows where such column is non-zero. However, my purpose is to obtain the list indices of the sparse matrices with such non-zero elements. I tried things like which(vmat[[]][,999] != 0) which(vmat[,,999] != 0) sapply(vmat, which, [,999] != 0) but none worked... any help will be appreciated!! Cheers, Germán [[alternative HTML version deleted]]
Hello Germ?n. You probably want something like: sapply(vmat, function(curMat){ curMat[,999] != 0 }) Or if you want the indices, just surround this with a which. HTH. Nick Sabbe -- ping: nick.sabbe at ugent.be link: http://biomath.ugent.be wink: A1.056, Coupure Links 653, 9000 Gent ring: 09/264.59.36 -- Do Not Disapprove -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Germ?n Sanchis Sent: woensdag 8 december 2010 11:50 To: r-help at r-project.org Subject: [R] problem accessing complex list data frames Hi all. I am currently attempting to build a list of sparse matrixes. That I have already achieved, by> vmat <- list() > for (i in 1:n) { > vmat <- c(vmat, sparseMatrix(i,j,x=data) }How I am trying to select those elements from the list where the column e.g. 999 is not null. I can do this for one of the sparse matrices with> which(vmat[[1]][,999] != 0)which returns the rows where such column is non-zero. However, my purpose is to obtain the list indices of the sparse matrices with such non-zero elements. I tried things like which(vmat[[]][,999] != 0) which(vmat[,,999] != 0) sapply(vmat, which, [,999] != 0) but none worked... any help will be appreciated!! Cheers, German [[alternative HTML version deleted]]
Thanks Nick! That's exactly what I wanted! The surrouding with a which was not exactly what I wanted, but which(apply(sapply(vmat, function(currMat){ currMat[,999] != 0 }),2,any)) did do the trick. Thank you very much!! Cheers, Germán 2010/12/8 Nick Sabbe <nick.sabbe@ugent.be>> Hello Germán. > > You probably want something like: > sapply(vmat, function(curMat){ > curMat[,999] != 0 > }) > Or if you want the indices, just surround this with a which. > > HTH. > > Nick Sabbe > -- > ping: nick.sabbe@ugent.be > link: http://biomath.ugent.be > wink: A1.056, Coupure Links 653, 9000 Gent > ring: 09/264.59.36 > > -- Do Not Disapprove > > > > -----Original Message----- > From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] > On > Behalf Of Germán Sanchis > Sent: woensdag 8 december 2010 11:50 > To: r-help@r-project.org > Subject: [R] problem accessing complex list data frames > > Hi all. > > I am currently attempting to build a list of sparse matrixes. That I have > already achieved, by > > > vmat <- list() > > for (i in 1:n) { > > vmat <- c(vmat, sparseMatrix(i,j,x=data) } > > How I am trying to select those elements from the list where the column > e.g. > 999 is not null. I can do this for one of the sparse matrices with > > > which(vmat[[1]][,999] != 0) > > which returns the rows where such column is non-zero. > > However, my purpose is to obtain the list indices of the sparse matrices > with such non-zero elements. I tried things like > > which(vmat[[]][,999] != 0) > which(vmat[,,999] != 0) > sapply(vmat, which, [,999] != 0) > > but none worked... any help will be appreciated!! > > Cheers, > > German > > [[alternative HTML version deleted]] > > >[[alternative HTML version deleted]]