Home > Software design >  how to make a new data set from original dataset with data only from a specific date
how to make a new data set from original dataset with data only from a specific date

Time:05-28

I have an original dataset called covid_cases and I need to make a new dataset called delta_cases bases on the covid_cases dataset with data only from the date 17th of august 2021 I have tried :

delta_cases <- covid_cases %>%
  filter(Date > '2021-08-17')

but that gave me no data

CodePudding user response:

Your Date column must be a Date class , you can use as.Date function something like :

covid_cases$Date <- as.Date(covid_cases$Date)

then you can compare dates with

delta_cases <- covid_cases %>% filter(Date > '2021-08-17')

Edit

Here is simple data set to help

library(dplyr , warn.conflicts = FALSE)

example <- data.frame(Date = c("2022-12-31" , "2022-12-30" ,"2022-12-29" , "2022-12-28")
                 , n_cases = c(12,23,12,34))
example$Date <- as.Date(example$Date)

delta_casses <- example %>% filter(Date > "2022-12-29")

delta_casses
#>         Date n_cases
#> 1 2022-12-31      12
#> 2 2022-12-30      23

Created on 2022-05-28 by the reprex package (v2.0.1)

CodePudding user response:

If str(covid_cases$Date) is indeed a date class, example %>% filter(Date > '2022-12-29') should work. You may have additional packages that are interfering with dplyr::filter(). Try running the following code to clear all packages and reload dplyr

invisible(lapply(
  paste0('package:', names(sessionInfo()$otherPkgs)),
  detach,
  character.only = TRUE,
  unload = TRUE
))
library(dplyr)
  •  Tags:  
  • r
  • Related