dplyr filter statement not in expression from a data.frame

给你一囗甜甜゛ 提交于 2020-01-06 06:01:26

问题


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


来源:https://stackoverflow.com/questions/50740791/dplyr-filter-statement-not-in-expression-from-a-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!