长数据帧到存在/不在场矩阵

时间:2019-03-06 12:53:58

标签: r dplyr tidyverse

我的数据帧很长,如下所示:

structure(list(guuid = c("sample 1", 
"sample 1", "sample 1", 
"sample 1", "sample 2", 
"sample 2"), gene = c("gene 1", "gene 2", 
"gene 3", "gene 4", "gene 5", "gene 6")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

我想将其转换为存在/不在场矩阵,例如:

        gene 1    gene 2    gene 3    gene 4    gene 5    gene 6
sample 1   1        0         1         0         0          0
sample 2   0        0         1         0         0          0

我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试一下:

table(dat)

给予:

          gene
guuid      gene 1 gene 2 gene 3 gene 4 gene 5 gene 6
  sample 1      1      1      1      1      0      0
  sample 2      0      0      0      0      1      1