Dear R users, I have a matrix like 85 .90 86 .89 87 .98 86 .87 88 .98 90 .78 88 .76 89 .56 90 .67 95 .67 89 .89 90 .87 91 .56 96 .87 90 .76 92.98 each pair of columns present a variable name and next the value. I have matrix with more than 500 rows and column. Now I want to convert this matrix in to. 85 .90 00 .00 00 86 .00 .89 .00 .87 87 .00 00 .98 00 88 .98 00 .76 00 89 .00 00 .89 .56 90 .67 .78 .76 .87 91 .56 00 .00 00 92 .00 .00 .00 .98 93 .00 .00 00 00 94 .00 .00 00 00 95 .00 .67 00 00 96 .00 .87 00 00 Where first column represent the Variable name. And if the first matrix is transposed, then how to get the output in which First raw present the variable name. It will be great help for my research. Dinesh -- Dinesh Kumar Barupal Research Associate Metabolomics Fiehn Lab UCD Genome Center 451 East Health Science Drive GBSF Builidng University of California DAVIS 95616 http://fiehnlab.ucdavis.edu/staff/kumar [[alternative HTML version deleted]]
Is this what you are after:> x <- read.table(textConnection("85 .90 86 .89 87 .98 86 .87+ 88 .98 90 .78 88 .76 89 .56 + 90 .67 95 .67 89 .89 90 .87 + 91 .56 96 .87 90 .76 92 .98"))> closeAllConnections() > odd <- seq(1, ncol(x), by=2) > x.r <- range(x[,odd]) > # create large array starting from 1 > x.m <- matrix(0, ncol=length(odd), nrow=max(x.r)) > for (i in odd){+ x.m[x[, i], (i + 1) / 2] <-x[, i + 1] # save values + }> # add first column with index > x.m <- cbind(seq(nrow(x.m)), x.m) > # only print out the rows of interest > x.m[x.r[1]:x.r[2],][,1] [,2] [,3] [,4] [,5] [1,] 85 0.90 0.00 0.00 0.00 [2,] 86 0.00 0.89 0.00 0.87 [3,] 87 0.00 0.00 0.98 0.00 [4,] 88 0.98 0.00 0.76 0.00 [5,] 89 0.00 0.00 0.89 0.56 [6,] 90 0.67 0.78 0.76 0.87 [7,] 91 0.56 0.00 0.00 0.00 [8,] 92 0.00 0.00 0.00 0.98 [9,] 93 0.00 0.00 0.00 0.00 [10,] 94 0.00 0.00 0.00 0.00 [11,] 95 0.00 0.67 0.00 0.00 [12,] 96 0.00 0.87 0.00 0.00 On Sat, Mar 29, 2008 at 7:21 PM, dinesh kumar <barupal at gmail.com> wrote:> Dear R users, > > I have a matrix like > > 85 .90 86 .89 87 .98 86 .87 > 88 .98 90 .78 88 .76 89 .56 > 90 .67 95 .67 89 .89 90 .87 > 91 .56 96 .87 90 .76 92.98 > > each pair of columns present a variable name and next the value. I have > matrix with more than 500 rows and column. > > Now I want to convert this matrix in to. > > 85 .90 00 .00 00 > 86 .00 .89 .00 .87 > 87 .00 00 .98 00 > 88 .98 00 .76 00 > 89 .00 00 .89 .56 > 90 .67 .78 .76 .87 > 91 .56 00 .00 00 > 92 .00 .00 .00 .98 > 93 .00 .00 00 00 > 94 .00 .00 00 00 > 95 .00 .67 00 00 > 96 .00 .87 00 00 > > Where first column represent the Variable name. > > And if the first matrix is transposed, then how to get the output in which > First raw present the variable name. > > It will be great help for my research. > > Dinesh > > -- > Dinesh Kumar Barupal > Research Associate > Metabolomics Fiehn Lab > UCD Genome Center > 451 East Health Science Drive > GBSF Builidng > University of California > DAVIS > 95616 > http://fiehnlab.ucdavis.edu/staff/kumar > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
try this: mat <- data.matrix(read.table( textConnection("85 .90 86 .89 87 .98 86 .87 88 .98 90 .78 88 .76 89 .56 90 .67 95 .67 89 .89 90 .87 91 .56 96 .87 90 .76 92 .98"))) closeAllConnections() nc <- ncol(mat) labs <- mat[, seq(1, nc, 2)] vals <- mat[, -seq(1, nc, 2)] lis <- split(c(vals, col(vals)), c(labs, labs)) t(sapply(lis, function(x, out){ nx <- length(x) ind <- seq(1, nx/2) out[x[-ind]] <- x[ind] out }, out = numeric(nc/2))) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting dinesh kumar <barupal at gmail.com>:> Dear R users, > > I have a matrix like > > 85 .90 86 .89 87 .98 86 .87 > 88 .98 90 .78 88 .76 89 .56 > 90 .67 95 .67 89 .89 90 .87 > 91 .56 96 .87 90 .76 92.98 > > each pair of columns present a variable name and next the value. I have > matrix with more than 500 rows and column. > > Now I want to convert this matrix in to. > > 85 .90 00 .00 00 > 86 .00 .89 .00 .87 > 87 .00 00 .98 00 > 88 .98 00 .76 00 > 89 .00 00 .89 .56 > 90 .67 .78 .76 .87 > 91 .56 00 .00 00 > 92 .00 .00 .00 .98 > 93 .00 .00 00 00 > 94 .00 .00 00 00 > 95 .00 .67 00 00 > 96 .00 .87 00 00 > > Where first column represent the Variable name. > > And if the first matrix is transposed, then how to get the output in which > First raw present the variable name. > > It will be great help for my research. > > Dinesh > > -- > Dinesh Kumar Barupal > Research Associate > Metabolomics Fiehn Lab > UCD Genome Center > 451 East Health Science Drive > GBSF Builidng > University of California > DAVIS > 95616 > http://fiehnlab.ucdavis.edu/staff/kumar > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm