How to build a matrix from a data frame based on the values ​​of a specific column?

I have a dataframe named dflike this:

Genes         ID          Type 
CFH         MB-0002       Gain 
CFHR3       MB-0002       Gain 
DEFB131     MB-0003       Gain 
UNC93B5     MB-0003       Loss 
CCDC125     MB-0004       Loss 
CCNB1       MB-0002       Gain
CFH         MB-0004       Loss
CCNB1       MB-0003       Gain   

I want to build a matrix, say Mat, and write it to a file csv, where I will have genes in the form of rows and columns IDs. I want to put:

  • 1if appropriate type Gain
  • -1if appropriate type Loss
  • 0 in all other places.

And an example of my matrix would be:

                MB-0002 MB-0003 MB-0004
   CFH              1       0      -1
   CFHR3            1       0       0
   DEFB131          0       1       0
   UNC93B5          0      -1       0
   CCDC125          0       0      -1
   CCNB1            1       1       0
+4
source share
1 answer

Try:

xtabs(c(1L, -1L)[Type] ~ ., data=df)
#         ID
#Genes     MB-0002 MB-0003 MB-0004
#  CCDC125       0       0      -1
#  CCNB1         1       1       0
#  CFH           1       0      -1
#  CFHR3         1       0       0
#  DEFB131       0       1       0
#  UNC93B5       0      -1       0

xtab()similar to table(), except that it takes a variable containing frequency numbers for each combination of levels. You can convert the result back to a data frame with as.data.frame().

"counts" ( , ). (. ?factor). . " ", Genes + ID.

+7

Source: https://habr.com/ru/post/1648429/


All Articles