简体   繁体   English

过滤匹配矢量的所有值的数据帧

[英]Filter data frame matching all values of a vector

I want to filter data frame x by including ID s that contain rows for Hour that match all values of testVector . 我想通过包含ID s来过滤数据框x ,这些ID包含与testVector 所有值匹配的Hour行。

ID <- c('A','A','A','A','A','B','B','B','B','C','C')
Hour <- c('0','2','5','6','9','0','2','5','6','0','2')

x <- data.frame(ID, Hour)
x
   ID Hour
1   A    0
2   A    2
3   A    5
4   A    6
5   A    9
6   B    0
7   B    2
8   B    5
9   B    6
10  C    0
11  C    2

testVector <- c('0','2','5')

The solution should yield the following data frame: 解决方案应该产生以下数据框:

x
       ID Hour
    1   A    0
    2   A    2
    3   A    5
    4   A    6
    5   A    9
    6   B    0
    7   B    2
    8   B    5
    9   B    6

All values of ID C were dropped because it was missing Hour 5. Note that I want to keep all values of Hour for ID s that match testVector . 的所有值ID ,因为它缺少C下降到Hour 5.请注意,我想保留的所有值HourID s表示比赛testVector

A dplyr solution would be ideal, but any solution is welcome. dplyr解决方案将是理想的,但任何解决方案都是受欢迎的。

Based on other related questions on SO, I'm guessing I need some combination of %in% and all , but I can't quite figure it out. 根据关于SO的其他相关问题,我猜我需要%in%all组合,但我无法弄明白。

Your combination of %in% and all sounds promising, in base R you could use those to your advantage as follows: 你的%in%all听起来很有希望的组合,在基础R你可以使用这些有利于你,如下:

to_keep = sapply(lapply(split(x,x$ID),function(x) {unique(x$Hour)}), 
                                              function(x) {all(testVector %in% x)})
x = x[x$ID %in% names(to_keep)[to_keep],]

Or similiarly, but skipping an unneccessary lapply and more efficient as per db in the comments: 或者类似地,但是在评论中根据db跳过一个不必要的lapply并且效率更高:

temp = sapply(split(x, x$ID), function(a) all(testVector %in% a$Hour))
x[temp[match(x$ID, names(temp))],]

Output: 输出:

  ID Hour
1  A    0
2  A    2
3  A    5
4  A    6
5  A    9
6  B    0
7  B    2
8  B    5
9  B    6

Hope this helps! 希望这可以帮助!

Here's another dplyr solution without ever leaving the pipe: 这是另一个没有离开管道的dplyr解决方案:

ID <- c('A','A','A','A','A','B','B','B','B','C','C')
Hour <- c('0','2','5','6','9','0','2','5','6','0','2')

x <- data.frame(ID, Hour)

testVector <- c('0','2','5')

x %>%
  group_by(ID) %>%
  mutate(contains = Hour %in% testVector) %>%
  summarise(all = sum(contains)) %>%
  filter(all > 2) %>%
  select(-all) %>%
  inner_join(x)

##       ID   Hour
##   <fctr> <fctr>
## 1      A      0
## 2      A      2
## 3      A      5
## 4      A      6
## 5      A      9
## 6      B      0
## 7      B      2
## 8      B      5
## 9      B      6

Here is an option using table from base R 这是一个使用base R table的选项

i1 <- !rowSums(table(x)[, testVector]==0)
subset(x, ID %in% names(i1)[i1])
#   ID Hour
#1  A    0
#2  A    2
#3  A    5
#4  A    6
#5  A    9
#6  B    0
#7  B    2
#8  B    5
#9  B    6

Or this can be done with data.table 或者这可以使用data.table完成

library(data.table)
setDT(x)[, .SD[all(testVector %in% Hour)], ID]
#    ID Hour
#1:  A    0
#2:  A    2
#3:  A    5
#4:  A    6
#5:  A    9
#6:  B    0
#7:  B    2
#8:  B    5
#9:  B    6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM