从某个数据帧中删除行,并减去R [duplicate]中另一个数据帧的值

问题描述 投票:0回答:2

我有这两个数据帧a和b我想从b

中删除a中的内容
example a =
     X  Y
1    1  3
2    2  4
3    3  5

example b = 
   X   Y   Z
1  3   5   4  --- want to remove this
2  4   6   2
3  1   3   2  --- want to remove this
4  2   3   4
5  5   3   4
6  2   4   2  ---  want to remove this
7  4   3   4
8  2   4   6 ----  want remove this
9  6   9   6
10 2   0   3

因此,我只保留没有组合的行最终结果将是:

   X   Y   Z
1  4   6   2
2  2   3   4
3  5   3   4
4  4   3   4
5  6   9   6
6  2   0   3

谢谢

r dataframe dplyr
2个回答
0
投票

anti-join包中的[dplyr可能非常有用。

library(tidyverse)

a <- tibble(X=c(1, 2, 3), Y=c(3, 4, 5))


b <- tibble(X=c(3, 4, 1, 2, 5, 2, 4, 2, 6, 2),
            Y=c(5, 6, 3, 3, 3, 4, 3, 4, 9, 0),
            Z=c(4, 2, 2, 4, 4, 2, 4, 6, 6, 3))

c <- b %>% anti_join(a, by=c("X", "Y"))
c

送礼

# A tibble: 6 x 3
      X     Y     Z
  <dbl> <dbl> <dbl>
1     4     6     2
2     2     3     4
3     5     3     4
4     4     3     4
5     6     9     6
6     2     0     3

-1
投票
© www.soinside.com 2019 - 2024. All rights reserved.