Convert a Matrix to Long Format.

matrix2long(mat)

Arguments

mat

A matrix to be converted into long format.

Value

A data frame in long format with columns: row, column, value, and id.

Details

This function converts a matrix into a long format data frame. The resulting data frame contains four columns: row, column, value, and id. The 'id' column assigns a unique identifier to each column group, making it easier to identify and analyze the data by column groups.

Examples

# Create a matrix
mat <- matrix(data = 1:9,
              nrow = 3,
              ncol = 3,
              dimnames = list(c("A", "B", "C"),
                              c("X", "Y", "Z")))

long_format <- matrix2long(mat)
long_format
#>   row_name col_name value id
#> 1        A        X     1  1
#> 2        B        X     2  1
#> 3        C        X     3  1
#> 4        A        Y     4  2
#> 5        B        Y     5  2
#> 6        C        Y     6  2
#> 7        A        Z     7  3
#> 8        B        Z     8  3
#> 9        C        Z     9  3

# Using correlation matrix
matrix2long(cor(mtcars))
#>     row_name col_name       value id
#> 1        mpg      mpg  1.00000000  1
#> 2        cyl      mpg -0.85216196  1
#> 3       disp      mpg -0.84755138  1
#> 4         hp      mpg -0.77616837  1
#> 5       drat      mpg  0.68117191  1
#> 6         wt      mpg -0.86765938  1
#> 7       qsec      mpg  0.41868403  1
#> 8         vs      mpg  0.66403892  1
#> 9         am      mpg  0.59983243  1
#> 10      gear      mpg  0.48028476  1
#> 11      carb      mpg -0.55092507  1
#> 12       mpg      cyl -0.85216196  2
#> 13       cyl      cyl  1.00000000  2
#> 14      disp      cyl  0.90203287  2
#> 15        hp      cyl  0.83244745  2
#> 16      drat      cyl -0.69993811  2
#> 17        wt      cyl  0.78249579  2
#> 18      qsec      cyl -0.59124207  2
#> 19        vs      cyl -0.81081180  2
#> 20        am      cyl -0.52260705  2
#> 21      gear      cyl -0.49268660  2
#> 22      carb      cyl  0.52698829  2
#> 23       mpg     disp -0.84755138  3
#> 24       cyl     disp  0.90203287  3
#> 25      disp     disp  1.00000000  3
#> 26        hp     disp  0.79094859  3
#> 27      drat     disp -0.71021393  3
#> 28        wt     disp  0.88797992  3
#> 29      qsec     disp -0.43369788  3
#> 30        vs     disp -0.71041589  3
#> 31        am     disp -0.59122704  3
#> 32      gear     disp -0.55556920  3
#> 33      carb     disp  0.39497686  3
#> 34       mpg       hp -0.77616837  4
#> 35       cyl       hp  0.83244745  4
#> 36      disp       hp  0.79094859  4
#> 37        hp       hp  1.00000000  4
#> 38      drat       hp -0.44875912  4
#> 39        wt       hp  0.65874789  4
#> 40      qsec       hp -0.70822339  4
#> 41        vs       hp -0.72309674  4
#> 42        am       hp -0.24320426  4
#> 43      gear       hp -0.12570426  4
#> 44      carb       hp  0.74981247  4
#> 45       mpg     drat  0.68117191  5
#> 46       cyl     drat -0.69993811  5
#> 47      disp     drat -0.71021393  5
#> 48        hp     drat -0.44875912  5
#> 49      drat     drat  1.00000000  5
#> 50        wt     drat -0.71244065  5
#> 51      qsec     drat  0.09120476  5
#> 52        vs     drat  0.44027846  5
#> 53        am     drat  0.71271113  5
#> 54      gear     drat  0.69961013  5
#> 55      carb     drat -0.09078980  5
#> 56       mpg       wt -0.86765938  6
#> 57       cyl       wt  0.78249579  6
#> 58      disp       wt  0.88797992  6
#> 59        hp       wt  0.65874789  6
#> 60      drat       wt -0.71244065  6
#> 61        wt       wt  1.00000000  6
#> 62      qsec       wt -0.17471588  6
#> 63        vs       wt -0.55491568  6
#> 64        am       wt -0.69249526  6
#> 65      gear       wt -0.58328700  6
#> 66      carb       wt  0.42760594  6
#> 67       mpg     qsec  0.41868403  7
#> 68       cyl     qsec -0.59124207  7
#> 69      disp     qsec -0.43369788  7
#> 70        hp     qsec -0.70822339  7
#> 71      drat     qsec  0.09120476  7
#> 72        wt     qsec -0.17471588  7
#> 73      qsec     qsec  1.00000000  7
#> 74        vs     qsec  0.74453544  7
#> 75        am     qsec -0.22986086  7
#> 76      gear     qsec -0.21268223  7
#> 77      carb     qsec -0.65624923  7
#> 78       mpg       vs  0.66403892  8
#> 79       cyl       vs -0.81081180  8
#> 80      disp       vs -0.71041589  8
#> 81        hp       vs -0.72309674  8
#> 82      drat       vs  0.44027846  8
#> 83        wt       vs -0.55491568  8
#> 84      qsec       vs  0.74453544  8
#> 85        vs       vs  1.00000000  8
#> 86        am       vs  0.16834512  8
#> 87      gear       vs  0.20602335  8
#> 88      carb       vs -0.56960714  8
#> 89       mpg       am  0.59983243  9
#> 90       cyl       am -0.52260705  9
#> 91      disp       am -0.59122704  9
#> 92        hp       am -0.24320426  9
#> 93      drat       am  0.71271113  9
#> 94        wt       am -0.69249526  9
#> 95      qsec       am -0.22986086  9
#> 96        vs       am  0.16834512  9
#> 97        am       am  1.00000000  9
#> 98      gear       am  0.79405876  9
#> 99      carb       am  0.05753435  9
#> 100      mpg     gear  0.48028476 10
#> 101      cyl     gear -0.49268660 10
#> 102     disp     gear -0.55556920 10
#> 103       hp     gear -0.12570426 10
#> 104     drat     gear  0.69961013 10
#> 105       wt     gear -0.58328700 10
#> 106     qsec     gear -0.21268223 10
#> 107       vs     gear  0.20602335 10
#> 108       am     gear  0.79405876 10
#> 109     gear     gear  1.00000000 10
#> 110     carb     gear  0.27407284 10
#> 111      mpg     carb -0.55092507 11
#> 112      cyl     carb  0.52698829 11
#> 113     disp     carb  0.39497686 11
#> 114       hp     carb  0.74981247 11
#> 115     drat     carb -0.09078980 11
#> 116       wt     carb  0.42760594 11
#> 117     qsec     carb -0.65624923 11
#> 118       vs     carb -0.56960714 11
#> 119       am     carb  0.05753435 11
#> 120     gear     carb  0.27407284 11
#> 121     carb     carb  1.00000000 11