one way is the following: A <- rbind(c(1,0,0,3), c(0,1,2,0), c(0,0,3,5)) t(apply(A, 1, function (x) x[x != 0]))
I hope it helps. Best, Dimitris lloyd barcza wrote:
I am trying to reduce the dimension of matrix by removing zero elements and creating a sub-matrix. For example: A= [1,0,0,3; 0,1,2,0; 0,0,3,5] then the new matrix B would be: B= [1,3;1,2;3,5] There are the same number of zero elements in each row of A so dimension of B will not be a problem. Is there a straightforward way to do this in R without writing a function using loops etc? Any help or suggestions would be appreciated! Thanks ______________________________________________ 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.
-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 ______________________________________________ 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.