Reputation: 403
I have a data frame df
like this:
df = data.frame("Gene1" = c("A","B","C","D"),
"Gene2" = c("B","E","NA","NA"),
"Gene3" = c("B","D","E","F"))
I wanted to transform it into a data frame with Gene
as the row names and all possible characters as the column names, showing "TRUE" or "FALSE" for matches. Something like this:
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
I am still learning R so could someone show me how to do this? Thanks!
Upvotes: 2
Views: 378
Reputation: 887153
A compact option would be 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
Upvotes: 2