R:如何从 table 中删除出现在另一个 table 中的值?
R: How to remove values from a table that appear in another table?
我有这样的数据:
> 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 解决方案,但我不确定如何比较表并根据另一个表从一个表中删除行。
感谢任何建议!
有很多方法可以做到这一点。
Base R 子集解决方案(如上文 Balter 所述):
M4M3.new <- M4M3[!(M4M3$gene_id %in% M4F4$gene_id),]
Base R集并解:
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",]
如果你想连接到数据库上的 运行,你也可以通过 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)
这样做的好处是您可以对大多数应用程序使用相同的语法,无论您的数据是在数据库中还是在本地数据框中。实际上 M4M3
和 M4F3
只是查询对象,而查询
仅在请求时才会 运行(例如,如果您显示数据或 运行 连接)。通过 collect()
:
转换为数据框
result_df <- anti_join(M4M3, M4F3) %>% collect()
在 introduction 中了解更多信息。
您可以像这样在一个 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
内括号中的代码 returns a table。
所以我们想要第一个 table 但不在第二个
中的所有 gene_id
我有这样的数据:
> 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 解决方案,但我不确定如何比较表并根据另一个表从一个表中删除行。
感谢任何建议!
有很多方法可以做到这一点。
Base R 子集解决方案(如上文 Balter 所述):
M4M3.new <- M4M3[!(M4M3$gene_id %in% M4F4$gene_id),]
Base R集并解:
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",]
如果你想连接到数据库上的 运行,你也可以通过 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)
这样做的好处是您可以对大多数应用程序使用相同的语法,无论您的数据是在数据库中还是在本地数据框中。实际上 M4M3
和 M4F3
只是查询对象,而查询
仅在请求时才会 运行(例如,如果您显示数据或 运行 连接)。通过 collect()
:
result_df <- anti_join(M4M3, M4F3) %>% collect()
在 introduction 中了解更多信息。
您可以像这样在一个 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
内括号中的代码 returns a table。
所以我们想要第一个 table 但不在第二个
gene_id