rdataframematrixz-score

How to convert a multiple columns matrix into a two-column table?


I have created a zscore matrix which looks like this

ColumnA L1 L2 L3 L4 L5
A 1.29 4.5 1.13 2.3
B 3.46 5.7
C 3 8.16 2.1 6.23

I want to convert it to a table of two columns, the columnA with names and the columnB with the values.

I have tried:

df <- as.data.frame(mx)
colnames(df) <- c('columnA', 'columnB')

which doesn't change the above matirx, only adds a new empty column (columnB) to it. basically all the values should be transveresed in one column (columnB) and the names in columnA should be repeated in each category. So I have also tried this based on a previous discussion on stackoverflow.

df <- mx %>% 
  mutate(ColumnA = as.data.frame(ColumnA)) %>%
  unnest_wider(ColumnA)

This also doesn't modify the mx.


Solution

  • You have different options to do this. Here some examples using different libraries. You can keep the variable with the labels L1, L2 and so on, and also filter the NA values from value using filter(!is.na(value))

    # Using library(reshape2)
    > reshape2::melt(mx, id.vars = "ColumnA", na.rm = TRUE)
       ColumnA variable value
    1        A       L1  1.29
    3        C       L1  3.00
    5        B       L2  3.46
    6        C       L2  8.16
    7        A       L3  4.50
    9        C       L3  2.10
    10       A       L4  1.13
    13       A       L5  2.30
    14       B       L5  5.70
    15       C       L5  6.23
    
    
    # Using library(tidyverse)
    > mx |> pivot_longer(-ColumnA, names_to = "vbl", values_to = "ColumnB")
    # A tibble: 15 × 3
       ColumnA vbl   ColumnB
       <chr>   <chr>   <dbl>
     1 A       L1       1.29
     2 A       L2      NA   
     3 A       L3       4.5 
     4 A       L4       1.13
     5 A       L5       2.3 
     6 B       L1      NA   
     7 B       L2       3.46
     8 B       L3      NA   
     9 B       L4      NA   
    10 B       L5       5.7 
    11 C       L1       3   
    12 C       L2       8.16
    13 C       L3       2.1 
    14 C       L4      NA   
    15 C       L5       6.23
    
    
    # Using library(tidyr)
    > tidyr::gather(mx, key = columnB, value = Value, -ColumnA)
       ColumnA columnB Value
    1        A      L1  1.29
    2        B      L1    NA
    3        C      L1  3.00
    4        A      L2    NA
    5        B      L2  3.46
    6        C      L2  8.16
    7        A      L3  4.50
    8        B      L3    NA
    9        C      L3  2.10
    10       A      L4  1.13
    11       B      L4    NA
    12       C      L4    NA
    13       A      L5  2.30
    14       B      L5  5.70
    15       C      L5  6.23