Home > Blockchain >  Filter Dates from Dataframe RStudio
Filter Dates from Dataframe RStudio

Time:02-04

I have seen multiple ways to filter out dates from a column within a dataframe but the outcome is not coming out the way I want. I'm looking for a way to remove the dates from the original dataset

  market_name      order_count   date year month day frequency wk
1    Twin Cities           3 2016-11-28 2016    11  28    Weekly 48
2    Twin Cities          11 2016-12-05 2016    12  05    Weekly 49
3    Twin Cities          22 2016-12-12 2016    12  12    Weekly 50
4    Twin Cities          59 2016-12-19 2016    12  19    Weekly 51
5    Twin Cities          58 2016-12-26 2016    12  26    Weekly 52

My dataset has a date column and I want to remove dates based on a condition. I have tried

dates <- 
  as.POSIXct(c("2020-03-16", "2020-03-23", "2020-03-30", "2020-04-6", "2020-04-13", 
               "2020-04-20", "2020-04-27", "2021-05-04", "2021-05-11", "2021-05-18",
               "2021-05-25","2021-06-01","2021-06-08"))

`%notin%` <- function(x,y) !(x %in% y) 

train_1 <- train %>%
  filter(date %notin% dates)

and also

train_1 <- train %>% subset(date >= '2020-04-15', date <= '2020-06-05' )

The subset function is actually removing the variables but keeping the observations.I also want to add the date column is in Date format based on the as.Date function

CodePudding user response:

you do not need a function. Try:

train[!train$date %in% dates, ]

as for getting a date range between

train[train$date >= '2020-04-15' & date <= '2020-06-05', ]
  •  Tags:  
  • Related