减去两个数据集

发布于 2025-01-03 17:23:49 字数 114 浏览 3 评论 0原文

我有 2 个数据集。一个是父数据集 (A),另一个是其子集 (B)。我想从 A 创建一个不包含 B 行的数据集。它应该类似于

C=AB

数据集 A 和 B 都具有相同的列数和列名称。

I have 2 datasets. One is the parent dataset (A) and other one is a subset (B) of it. I want to create a dataset from A which does not contain rows from B. It should be something like

C=A-B

Both the datasets A and B have same number of columns and column names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2025-01-10 17:23:49

如果 B 是 A 的实际子集,则可以在 rownames 上使用 setdiff

sset <- subset(mtcars,cyl==4)
mtcars[setdiff(rownames(mtcars),rownames(sset)),]

If B is an actual subset of A, you can use setdiff on rownames:

sset <- subset(mtcars,cyl==4)
mtcars[setdiff(rownames(mtcars),rownames(sset)),]
蓝咒 2025-01-10 17:23:49

如果不想将其转换为字符串进行比较,即进行精确匹配
你可以试试这个

a <- data.frame(t(matrix(1:12,3,4)))
b <- data.frame(t(matrix(7:21,3,5)))
a[!apply(a,1,FUN=function(y){any(apply(b,1,FUN=function(x){all(x==y)}))}),]

If you do not want to convert it into a string for comparing, i.e Do exact matches
you can try this out

a <- data.frame(t(matrix(1:12,3,4)))
b <- data.frame(t(matrix(7:21,3,5)))
a[!apply(a,1,FUN=function(y){any(apply(b,1,FUN=function(x){all(x==y)}))}),]
栖竹 2025-01-10 17:23:49

像下面这样的东西可能会起作用:

C <- A[!(apply(A, 1, toString) %in% apply(B, 1, toString)), ]

Something like the following might do the trick:

C <- A[!(apply(A, 1, toString) %in% apply(B, 1, toString)), ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文