Hi list I have a matrix of size m x n (m and n are different, hence non square!) I want to melt it in such a way that I get a df of 3 columns. m ,n and cell value in the original matrix. Any suggestions? -- *Chirag Gupta* Department of Crop, Soil, and Environmental Sciences, 115 Plant Sciences Building, Fayetteville, Arkansas 72701 [[alternative HTML version deleted]]
On 01/03/14 14:49, Chirag Gupta wrote:> Hi list > > I have a matrix of size m x n (m and n are different, hence non square!) > I want to melt it in such a way that I get a df of 3 columns. m ,nSurely you mean i, j (i = 1, ..., m), j = 1, ..., n).> and cell > value in the original matrix.Yes: dfX <- data.frame(i=as.vector(row(X)),j=as.vector(col(X)), value=as.vector(X)) where X is your matrix. The fact that X is non-square is of course completely irrelevant. cheers, Rolf Turner
library(reshape2) mx <- matrix( 1:12, nrow=3 ) mxdf <- melt( mx ) names( mxdf ) <- c( "m", "n", "value" ) --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On February 28, 2014 5:49:26 PM PST, Chirag Gupta <cxg040 at email.uark.edu> wrote:>Hi list > >I have a matrix of size m x n (m and n are different, hence non >square!) >I want to melt it in such a way that I get a df of 3 columns. m ,n and >cell >value in the original matrix. > >Any suggestions?
Hi, You could try: #If mat1 is the matrix dimnames(mat1) <- list(1:nrow(mat1),1:ncol(mat1)) setNames(as.data.frame.table(mat1),c("m","n","value")) A.K. On Friday, February 28, 2014 11:40 PM, Chirag Gupta <cxg040 at email.uark.edu> wrote: Hi list I have a matrix of size m x n (m and n are different, hence non square!) I want to melt it in such a way that I get a df of 3 columns. m ,n and cell value in the original matrix. Any suggestions? -- *Chirag Gupta* Department of Crop, Soil, and Environmental Sciences, 115 Plant Sciences Building, Fayetteville, Arkansas 72701 ??? [[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.