R:如何从另一个表中出现的表中删除值?

时间:2018-01-10 19:34:59

标签: r rsqlite

我的数据如下:

> head(dbGetQuery(mydb, 'SELECT * FROM geneExpDiffData WHERE significant = "yes"'))
      gene_id sample_1 sample_2 status value_1 value_2 log2_fold_change test_stat p_value   q_value significant
1 XLOC_000219       M4       M3     OK 3.85465 0.00000             -Inf        NA   5e-05 0.0075951         yes
2 XLOC_004272       M4       M3     OK 2.06687 0.00000             -Inf        NA   5e-05 0.0075951         yes
3 XLOC_004991       M4       M3     OK 3.29904 0.00000             -Inf        NA   5e-05 0.0075951         yes
4 XLOC_007234       M4       M3     OK 1.28027 0.00000             -Inf        NA   5e-05 0.0075951         yes
5 XLOC_000664       M4       F4     OK 1.46853 0.00000             -Inf        NA   5e-05 0.0075951         yes
6 XLOC_001809       M4       F4     OK 0.00000 1.91743              Inf        NA   5e-05 0.0075951         yes

我用RSQLite制作了两个子集:

M4M3 <- dbGetQuery(mydb, 'SELECT * FROM geneExpDiffData WHERE significant = "yes" AND sample_1 = "M4" AND sample_2 = "M3"')

M4F4 <- dbGetQuery(mydb, 'SELECT * FROM geneExpDiffData WHERE significant = "yes" AND sample_1 = "M4" AND sample_2 = "F4"')

我想删除M4M3中M4F4中匹配的gene_id的所有值。使用RSQLite过滤数据集并不重要,它可能是一个纯R解决方案,但我不确定如何比较表并从一个基于另一个删除行。

感谢您的任何建议!

3 个答案:

答案 0 :(得分:2)

有很多方法可以做到这一点。

Base R子集解决方案(如上面Balter所述):

M4M3.new <- M4M3[!(M4M3$gene_id %in% M4F4$gene_id),]

Base R set union solution:

M4M3.new <- setdiff(M4M3, M4F4)

Dplyr解决方案

M4M3.new <- dplyr::anti_join(M4M3, 
                             M4F4, 
                             by = c("gene_id" = "gene_id"))

编辑:所有似乎都在以下数据集上进行了测试:

tst1 <- data.frame(gene_id = seq(1:10), 
                   sample_1 = rep("M4", 10), 
                   sample_2 = c(rep("M3", 6), rep("F4", 4)), 
                   other_values = sample(1:10, 10, replace = T),
                   other_values2 = rep("OK", 10))

M4M3 <- tst1[tst1$sample_1 == "M4" & tst1$sample_2  == "M3",]
M4F4 <- tst1[tst1$sample_1 == "M4" & tst1$sample_2  == "F4",]

答案 1 :(得分:1)

如果要在数据库上运行联接,也可以通过dbplyr连接:

library(dplyr)
src <- dbplyr::src_dbi(db)
geneExpDiffData <- tbl(src, "geneExpDiffData")

M4M3 <- geneExpDiffData %>%
  filter(significant == "yes" & sample_1 == "M4" & sample_2 == "M3")

M4F3 <- geneExpDiffData %>%
  filter(significant == "yes" & sample_1 == "M4" & sample_2 == "F4")

anti_join(M4M3, M4F3)

这样做的好处是,无论数据是在数据库中还是在本地数据框中,您都可以对大多数应用程序使用相同的语法。事实上,M4M3M4F3只是查询对象和查询 将仅在请求时运行(例如,如果您显示数据或运行连接)。通过collect()转换为数据框:

result_df <- anti_join(M4M3, M4F3) %>% collect()

了解详情introduction

答案 2 :(得分:1)

您可以直接在一个SQL语句中执行此操作:

M4M3 <- dbGetQuery(mydb, '
SELECT * 
FROM geneExpDiffData 
WHERE significant = "yes" 
AND sample_1 = "M4" 
AND sample_2 = "M3"
AND gene_id not in (SELECT gene_id 
                    FROM geneExpDiffData 
                    WHERE significant = "yes" 
                    AND sample_1 = "M4" 
                    AND sample_2 = "F4")
')

内括号中的代码返回M4F4中所有gene_id的表格。 所以我们希望第一个表中的所有gene_id而不是第二个表中的Financials: Date CountryID ProductID Revenue Cost