[R] reshape non-square matrix

arun smartpink111 at yahoo.com
Wed Mar 5 05:00:51 CET 2014





Hi,
You didn't provide any reproducible example to start with.
Using Jeff's example

mx <- matrix( 1:12, nrow=3 )
dimnames(mx) <- list(1:nrow(mx),1:ncol(mx)) ##
setNames(as.data.frame.table(mx),c("m","n","value"))

  m n value
1  1 1     1
2  2 1     2
3  3 1     3
4  1 2     4
5  2 2     5
6  3 2     6
7  1 3     7
8  2 3     8
9  3 3     9
10 1 4    10
11 2 4    11
12 3 4    12

mx1 <- matrix(1:12,nrow=3,ncol=4,dimnames=list(1:3,c("Col1","Col2","Col3","Col4")))


setNames(as.data.frame.table(mx1),c("m","n","value"))
   m    n value
1  1 Col1     1
2  2 Col1     2
3  3 Col1     3
4  1 Col2     4
5  2 Col2     5
6  3 Col2     6
7  1 Col3     7
8  2 Col3     8
9  3 Col3     9
10 1 Col4    10
11 2 Col4    11
12 3 Col4    12

A.K.



On Tuesday, March 4, 2014 9:39 PM, Chirag Gupta <cxg040 at email.uark.edu> wrote:

This takes away names of all the variables



On Sat, Mar 1, 2014 at 9:30 AM, arun <smartpink111 at yahoo.com> wrote:


>
>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.
>
>


-- 
Chirag Gupta
Department of Crop, Soil, and Environmental Sciences,
115 Plant Sciences Building, Fayetteville, Arkansas 72701




More information about the R-help mailing list