如果一行在R列中共享另一行的值并且在R中的另一列中具有一个值,如何删除该行?

时间:2019-06-01 04:44:22

标签: r filter subset

我有一个以下数据框,我们称之为服务器。

Server <-c("AUSTRALIA", "AUSTRALIA", "AUSTRALIA", "AUSTRALIA", "AUSTRALIA","CHINA","COLOMBIA","COLOMBIA","ECUADOR","ECUADOR","ECUADOR","COREA","COREA","COREA","COREA","BRASIL","BRASIL","BRASIL","ALEMANIA","ALEMANIA","ALEMANIA","ALEMANIA") Product <- c("021-08183","79P-01747","C5E-00746","7NQ-00302","7JQ-00341","021-08183","021-08183","79P-01747","79P-01747","021-08183","6QH-00002","79P-01747","021-08183","#N/A","6QH-00002","79P-01747","76P-00742","021-08183","021-08183","79P-01747","C5E-00746","7NQ-00302") Servers <- data.frame(Server, Product)

看起来像这样

Table

我需要以下内容。每当我在Server中具有相同的值,以及值021-08183和79P-01747时,我都需要删除具有021-08183值的行,而只保留具有79P-01747的那一行。

输出:

enter image description here

非常感谢!

PS:对不起,我在为原始表格上传图片时遇到了一些问题

2 个答案:

答案 0 :(得分:1)

这是使用dplyr软件包的一种方法-

Servers %>% 
  group_by(Server) %>% 
  filter(
    (all(c("021-08183", "79P-01747") %in% Product) & (Product != "021-08183")) | n() == 1
  ) %>% 
  ungroup()

# A tibble: 16 x 2
   Server    Product  
   <chr>     <chr>    
 1 AUSTRALIA 79P-01747
 2 AUSTRALIA C5E-00746
 3 AUSTRALIA 7NQ-00302
 4 AUSTRALIA 7JQ-00341
 5 CHINA     021-08183
 6 COLOMBIA  79P-01747
 7 ECUADOR   79P-01747
 8 ECUADOR   6QH-00002
 9 COREA     79P-01747
10 COREA     #N/A     
11 COREA     6QH-00002
12 BRASIL    79P-01747
13 BRASIL    76P-00742
14 ALEMANIA  79P-01747
15 ALEMANIA  C5E-00746
16 ALEMANIA  7NQ-00302

答案 1 :(得分:0)

如果同时显示“ 021-08183”和“ 79P-01747”,则按“服务器”分组后filter在产品列中具有“ 021-08183”的行中创建逻辑条件在列中

library(dplyr)
Servers %>%
   group_by(Server) %>%
   filter(if(all(c("021-08183", "79P-01747") %in% Product)) 
     Product != "021-08183" else TRUE)
# A tibble: 16 x 2
# Groups:   Server [7]
#   Server    Product  
#   <fct>     <fct>    
# 1 AUSTRALIA 79P-01747
# 2 AUSTRALIA C5E-00746
# 3 AUSTRALIA 7NQ-00302
# 4 AUSTRALIA 7JQ-00341
# 5 CHINA     021-08183
# 6 COLOMBIA  79P-01747
# 7 ECUADOR   79P-01747
# 8 ECUADOR   6QH-00002
# 9 COREA     79P-01747
#10 COREA     #N/A     
#11 COREA     6QH-00002
#12 BRASIL    79P-01747
#13 BRASIL    76P-00742
#14 ALEMANIA  79P-01747
#15 ALEMANIA  C5E-00746
#16 ALEMANIA  7NQ-00302

或带有data.table

的类似选项
library(data.table)
setDT(Servers)[, .SD[if(all(c("021-08183", "79P-01747") %in% Product)) 
        Product != "021-08183" else TRUE], Server]

注意:它提供了与OP相同的预期输出,同时保留了“ CHINA”的行