Используйте merge
, а затем dcast
из пакета reshape2
.
res <- merge(df_2[2], df_1, all.x = TRUE)
res$Sample_name <- as.character(res$Sample_name)
res$Sample_name[is.na(res$Sample_name)] <- ""
reshape2::dcast(res, symbol ~ Sample_name, value.var = "Sample_name")
# symbol Var.2 sample_1 sample_3
#1 MTPAP 0 3 0
#2 POLR1A 0 0 1
#3 POLR1B 1 0 0
#4 POLR1C 1 0 0
#5 TENT2 0 1 0
#6 TENT4A 1 0 0
#7 TENT4B 1 0 0
#8 TUT1 1 0 0
#9 TUT4 1 0 0
#10 TUT7 1 0 0
Данные в формате dput
.
df_1 <-
structure(list(symbol = structure(c(5L, 5L, 5L,
7L, 4L, 6L, 2L, 3L, 1L, 8L), .Label = c("ATF2",
"CCDC138", "CCDC74A", "KIDINS220", "MTPAP",
"POLR1A", "TENT2", "TLR9"), class = "factor"),
Sample_name = structure(c(1L, 1L, 1L, 1L, 2L,
3L, 4L, 5L, 6L, 7L), .Label = c("sample_1", "sample_2",
"sample_3", "sample_4", "sample_5", "sample_6",
"sample_7"), class = "factor")),
class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10"))
df_2 <-
structure(list(HGNC.ID = structure(c(5L,
8L, 1L, 10L, 7L, 9L, 6L, 2L, 4L, 3L),
.Label = c("HGNC:16705", "HGNC:17264", "HGNC:20194",
"HGNC:20454", "HGNC:25532", "HGNC:25817",
"HGNC:26184", "HGNC:26776", "HGNC:28981",
"HGNC:30758"), class = "factor"),
symbol = structure(c(1L, 5L, 6L, 7L, 8L,
9L, 10L, 2L, 3L, 4L), .Label = c("MTPAP", "POLR1A",
"POLR1B", "POLR1C", "TENT2", "TENT4A", "TENT4B",
"TUT1", "TUT4", "TUT7"), class = "factor")),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))