Search code examples
rreshape

how to transform double matrix into dataframe columns


i'm trying to reshape some square double matrix into some columns dataframe

something like:

                genomeA    genomeB    genomeC
       genomeA    1.0        0.5       0.3
       genomeB    0.5        1.0       0.2
       genomeC    0.3        0.2       1.0

into

Genomes1   Genomes2    Value
genomeA    genomeA     1.0
genomeA    genomeB     0.5
genomeA    genomeC     0.3
 .....

i tried to cast it into a dataframe but it didn't do anything i tried with tibble

    df <- corr %>%
  as_tibble() %>%
  setNames(c('GenomesA', 'GenomesB', 'AAI'))

it created some strange matrix but not what i want

if someone as a clue for me! thanks


Solution

  • You can use array2DF (introduced in R 4.3.0):

    array2DF(m)
    

    But this has the unintended consequence to output an incorrect second column; you can remedy this like so:

    array2DF(m, responseName = "AAI", simplify = FALSE) |>
      transform(AAI = mapply(\(x, y) x[y], AAI, match(Var2, unique(Var2))))
    
    #      Var1    Var2 AAI
    # 1 genomeA genomeA 1.0
    # 2 genomeB genomeA 0.5
    # 3 genomeC genomeA 0.3
    # 4 genomeA genomeB 0.5
    # 5 genomeB genomeB 1.0
    # 6 genomeC genomeB 0.2
    # 7 genomeA genomeC 0.3
    # 8 genomeB genomeC 0.2
    # 9 genomeC genomeC 1.0
    

    data

    m <- read.table(h=T,text="         genomeA    genomeB    genomeC
           genomeA    1.0        0.5       0.3
           genomeB    0.5        1.0       0.2
           genomeC    0.3        0.2       1.0")