dplyr filter statement not in expression from a da

2019-08-27 22:46发布

问题:

I would like to use not in statement with a data.frame in dplyr but it is not working. I would like to exclude values from a data.frame since I do have huge week numbers. Below is an example

df1 = data.frame(week=c(1,2,3,4,5,6),sales=c(10,24,23,54,65,45))
week_e=data.frame(week=c(2,5)) 

so I would like to exclude weeks in week_e data frame from df1 and below is code and it is not working. Please help! Thank you.

       df1  %>%
       filter(!week %in% week_e)  

       week sales
   1    1    10
   2    2    24
   3    3    23
   4    4    54
   5    5    65
   6    6    45

回答1:

Actually I got the answer. Add an unlist to the week_e data.frame then it is solved

 df1 = data.frame(week=c(1,2,3,4,5,6),sales=c(10,24,23,54,65,45))
 week_e=unlist(data.frame(week=c(2,5)))

 df1  %>%
 filter(!week %in% week_e) 

 week sales
  1    10
  3    23
  4    54
  6    45