, , . A
. data.table
CRAN (v 1.9.6 +)
library(data.table) # V 1.9.6+
res <- melt(setDT(A), id = "Pathway")[setDT(B), Value := i.Value, on = c(value = "Gene")]
dcast(res, Pathway ~ variable, value.var = "Value")
# Pathway v1 v2
# 1: 1 500 1000
# 2: 2 300 500
# 3: 3 NA 200
# 4: 4 NA NA
Hadleyverse
library(dplyr)
library(tidyr)
A %>%
gather(res, Gene, -Pathway) %>%
left_join(., B, by = "Gene") %>%
select(-Gene) %>%
spread(res, Value)
# Pathway v1 v2
# 1 1 500 1000
# 2 2 300 500
# 3 3 NA 200
# 4 4 NA NA
A <- structure(list(Pathway = 1:4, v1 = structure(1:4, .Label = c("A",
"B", "C", "D"), class = "factor"), v2 = structure(c(2L, 1L, 3L,
4L), .Label = c("A", "E", "G", "K"), class = "factor")), .Names = c("Pathway",
"v1", "v2"), class = "data.frame", row.names = c(NA, -4L))
B <- structure(list(Gene = structure(c(3L, 1L, 4L, 2L), .Label = c("A",
"B", "E", "G"), class = "factor"), Value = c(1000L, 500L, 200L,
300L)), .Names = c("Gene", "Value"), class = "data.frame", row.names = c(NA,
-4L))