Search code examples
rtidyverse

how to drop multiple days from dataset


I am able to drop one day only from dataset using filter, However, I can't find a way to drop multiple days. See below:

a <- structure(list(year = c("2011", "2011", "2011", "2011", "2011", 
"2011", "2011", "2011", "2011", "2011", "2012", "2012", "2012", 
"2012", "2012", "2012", "2012", "2012", "2012"), month = structure(c(8L, 
8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 8L, 8L, 9L, 9L, 10L, 
10L, 11L, 11L, 12L), .Label = c("January", "February", "March", 
"April", "May", "June", "July", "August", "September", "October", 
"November", "December"), class = "factor"), sixteen = structure(c(15187, 
15203, 15218, 15234, 15248, 15264, 15279, 15295, 15309, 15325, 
15553, 15569, 15584, 15600, 15614, 15630, 15645, 15661, 15675
), class = "Date")), class = "data.frame", row.names = c(NA, 
-19L))

year     month    sixteen
1  2011    August 2011-08-01
2  2011    August 2011-08-17
3  2011 September 2011-09-01
4  2011 September 2011-09-17
5  2011   October 2011-10-01
6  2011   October 2011-10-17
7  2011  November 2011-11-01
8  2011  November 2011-11-17
9  2011  December 2011-12-01
10 2011  December 2011-12-17
11 2012    August 2012-08-01
12 2012    August 2012-08-17
13 2012 September 2012-09-01
14 2012 September 2012-09-17
15 2012   October 2012-10-01
16 2012   October 2012-10-17
17 2012  November 2012-11-01
18 2012  November 2012-11-17
19 2012  December 2012-12-01

    
    #drop days
    days <- c('2011-12-17', '2012-12-01', '2012-11-17')

#I used:
subset(a, !as.Date(sixteen) %in% days) #doesn't work

a[!(a$sixteen %in% days),]     #Does not work either

Solution

  • You'll need to make sure the strings you're filtering for are also data type of date, rather than column sixteen, i.e.

    days <- as.Date(c('2011-12-17', '2012-12-01', '2012-11-17'))
    
       year     month    sixteen
    1  2011    August 2011-08-01
    2  2011    August 2011-08-17
    3  2011 September 2011-09-01
    4  2011 September 2011-09-17
    5  2011   October 2011-10-01
    6  2011   October 2011-10-17
    7  2011  November 2011-11-01
    8  2011  November 2011-11-17
    9  2011  December 2011-12-01
    11 2012    August 2012-08-01
    12 2012    August 2012-08-17
    13 2012 September 2012-09-01
    14 2012 September 2012-09-17
    15 2012   October 2012-10-01
    16 2012   October 2012-10-17
    17 2012  November 2012-11-01