从这两个数据帧开始:
data <- data.frame("Run_ID" = c(1,2,3), "Sample" = c("A", "B", "C"), "Value" = c(1,2,3))
metadata <- data.frame("Run_ID" = c(1,3), "Sample" = c("A","C"))
我想对data
进行子集划分,使其仅包括来自Run_ID
+Sample
对的值,这些值也存在于metadata
中。输出应包含与data
相同的列。
预期输出:
Run_ID Sample Value
1 A 1
3 C 3
根据文档,似乎semi_join()
应该是解决方案,但我无法基于这两个变量来计算联接。
>semi_join(data, metadata, by = c("Run_ID", "Sample"))
[1] Run_ID Sample Value
<0 rows> (or 0-length row.names)
非常感谢您的任何建议!
您的代码还可以,但输入的metadata
不是友好的格式,但我想这就是您想要的:
semi_join(
data,
metadata %>% separate_rows(Sample, sep = ','),
by = c('Run_ID', 'Sample')
)
# Run_ID Sample Value
# 1 1 A 1
# 2 3 C 3
这行吗:
library(dplyr)
library(tidyr)
metadata %>% separate_rows(Sample) %>% inner_join(data)
Joining, by = c("Run_ID", "Sample")
# A tibble: 2 x 3
Run_ID Sample Value
<dbl> <chr> <dbl>
1 1 A 1
2 3 C 3