I have two data frame y
and z
y <- data.frame(ID = c("A", "A", "A", "B", "B"), gene = c("a", "b", "c", "a", "c"))
z <- data.frame(A = c(2,6,3), B = c(8,4,9), C=c(1,6,2))
rownames(z) <- c("a", "b", "c")
So for y I have a table with patients ID and gene for each patients and in Z I have the same patients IDs in the first row and a list of genes with a specific value (which is not important here). The genes in y are in z, but in z there are genes that are not included in y.
What I want to do is to merge
this frames and have something like this:
a b c
A 1 1 1
B 1 0 1
So for each patient, if the genes in z
are also in y,
fill with 1 and if not, fill with 0
I don't really know how to handle this, any ideas? Thank you
A
,B
,C
columns inz
have any relation to the result? - Gregor Thomasy
andz
object. Look at the functiondput
, it might help. - Bastientidyr::pivot_wider
ortidyr::pivot_longer
. If you're more old school,reshape2::dcast
orreshape2::melt
. - Bastien