Patrick, thank you. This works. I can't believe I have wasted several hours in front of computer for something this easy! :))
2011/6/21 Patrick Breheny [via R] < ml-node+3614530-1847060061-246...@n4.nabble.com> > On 06/21/2011 09:48 AM, davefrederick wrote: > > Hi, I have a 500x 53380 sparse matrix and I am trying to dichotomize it. > > Under sna package I have found event2dichot yet it doesnt recognize > sparse > > matrix and requires adjacency matrix or array. I tried to run a simple > loop > > code > > > > for (i in 1:500) > > for (j in 1:53380) > > if (matrix[i,j]>0) matrix[i,j]=1 > > The code you are running does not require a loop: > > > A <- cbind(c(1,0),c(0,2)) > > A > [,1] [,2] > [1,] 1 0 > [2,] 0 2 > > A[A>0] <- 1 > > A > [,1] [,2] > [1,] 1 0 > [2,] 0 1 > > However, for large sparse matrices, this and other operations will be > faster if the matrix is explicitly stored as a sparse matrix, as > implemented in the 'Matrix' package. > > -- > Patrick Breheny > Assistant Professor > Department of Biostatistics > Department of Statistics > University of Kentucky > > ______________________________________________ > [hidden email] <http://user/SendEmail.jtp?type=node&node=3614530&i=0>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. > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://r.789695.n4.nabble.com/working-with-sparse-matrix-tp3614117p3614530.html > To unsubscribe from working with sparse matrix, click > here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3614117&code=em9ybHVoYW5AZ21haWwuY29tfDM2MTQxMTd8LTg2ODk2MDE2NA==>. > > -- View this message in context: http://r.789695.n4.nabble.com/working-with-sparse-matrix-tp3614117p3614689.html Sent from the R help mailing list archive at Nabble.com. [[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.