如何返回" TRUE" /" FALSE"对于数据框中的匹配?

时间:2018-05-08 16:56:06

标签: r dataframe

我有一个像这样的数据框df

df = data.frame("Gene1" = c("A","B","C","D"),
                "Gene2" = c("B","E","NA","NA"),
                "Gene3" = c("B","D","E","F"))

我想将其转换为数据框,其中Gene作为行名称,所有可能的字符作为列名称,显示" TRUE"或者" FALSE"比赛。像这样:

           A      B      C      D      E      F
Gene1   TRUE   TRUE   TRUE   TRUE  FALSE  FALSE
Gene2  FALSE   TRUE  FALSE  FALSE   TRUE  FALSE
Gene3  FALSE   TRUE  FALSE   TRUE   TRUE   TRUE

我还在学习R所以有人可以告诉我该怎么做吗?谢谢!

1 个答案:

答案 0 :(得分:2)

紧凑选项为mtabulate

library(qdapTools)
mtabulate(df)[-6]!=0  #removed the 6th column as it is NA
#          A    B     C     D     E     F
#Gene1  TRUE TRUE  TRUE  TRUE FALSE FALSE
#Gene2 FALSE TRUE FALSE FALSE  TRUE FALSE
#Gene3 FALSE TRUE FALSE  TRUE  TRUE  TRUE