2

从这两个数据帧开始:

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)

任何建议都非常感谢!

4

2 回答 2

2

您的代码没问题,但输入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
于 2021-06-24T10:04:12.467 回答
0

这是否有效:

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
于 2021-06-24T09:19:55.933 回答