Home > OS >  Convert list of matrices in to dataframe
Convert list of matrices in to dataframe

Time:09-03

I have a list of matrices that I would like to convert into a list of data frames. I want the data frame to have 1 columns with the matrix number, the second column to have the row-col number and the value inside the matrix inside the third column. I had initially converted a dataframe with the counts to a matrix:

G <- df%>%
  select(c('[run number]',distribution,'who-mutgroom1','who-mutgroom2','mut-groom'))

Glist <- vector("list",80)
for(run in 1:80){
  newmatrix <- matrix(nrow = 10, ncol = 10)
  for(x in 1:90){
    Actor = G$`who-mutgroom1`[G$`[run number]` == run][x]   1
    Receiver =G$`who-mutgroom2`[G$`[run number]` == run][x]   1
    MGroom = G$`mut-groom`[G$`[run number]` == run][x]
    newmatrix[Actor,Receiver] = as.numeric(MGroom)
  }
  newmatrix[is.na(newmatrix)] <- 0
  Glist[[run]] <- newmatrix
}

And then I wanted to get counts for all interactions(0 has to be included :

[79]]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    6   26    8   12    6   11    7    6     3
 [2,]    6    0   11   10   13    8   12   14    8    10
 [3,]   26   11    0    8   12    4   10   11    3     4
 [4,]    8   10    8    0    3    3    8   16    9     8
 [5,]   12   13   12    3    0    8   12    6   17    17
 [6,]    6    8    4    3    8    0   10   13   21    23
 [7,]   11   12   10    8   12   10    0   25   19    23
 [8,]    7   14   11   16    6   13   25    0   15     7
 [9,]    6    8    3    9   17   21   19   15    0    29
[10,]    3   10    4    8   17   23   23    7   29     0

[[80]]
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0   15    9    8   10   11   23    5    5     6
 [2,]   15    0    9   11    7   13   13   12   15     6
 [3,]    9    9    0   18    7   12    7   13   14    24
 [4,]    8   11   18    0    6   15   17   10   10    29
 [5,]   10    7    7    6    0   22   12   20    6    10
 [6,]   11   13   12   15   22    0   16   28   28    22
 [7,]   23   13    7   17   12   16    0   11   11    15
 [8,]    5   12   13   10   20   28   11    0   21    23
 [9,]    5   15   14   10    6   28   11   21    0    20
[10,]    6    6   24   29   10   22   15   23   20     0

so the output needs to be like :

[run] Actor-Receiver Count
    80     1-2         15
    80     1-3         9
   

CodePudding user response:

When you share data, it's always important to use something like dput or reprex::reprex.

I created 80 copies of the first matrix to make a list of matrices named G.

Then I used the following to build the data frame.

df1 <- data.frame("run" = integer(), "Actor_Receiver" = character(),
                  "Count" = integer())
for(run in 1:80) {               # matrices
  for(rs in 1:10) {              # each row in each matrix
    for(cs in 1:10) {            # each column in each matrix
      df1[nrow(df1)   1, ] <- c(run, paste0(rs, "-", cs), 
                                G[[run]][rs, cs])
    }
  }
}

This is what the output would look like.

head(df1)
#   run Actor_Receiver Count
# 1   1            1-1     0
# 2   1            1-2     6
# 3   1            1-3    26
# 4   1            1-4     8
# 5   1            1-5    12
# 6   1            1-6     6 
  • Related