2

I want use a list of years to filter a database by date

years<-c("2014")
yearsdata <- data.frame(animal=c("cow","pig"),
                        mydate=c(as.Date("2015-01-01"),
                        as.Date("2014-01-01")))
yearsdata %>% 
          mutate(mydate =format(mydate, "%Y") %>% 
          as.character()) %>%     
          filter(is.null(years) | mydate %in% years)

The above code works and lets me filter my dataset but it also formats the date column. Is there a way to get my filter results without having the format of the date column change in the finished subset dataframe?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
BaconDoggie
  • 153
  • 3
  • 14
  • If you don't want to format the date, then don't overwrite `mydate` with `format(mydate)`. Just make a new column `year = format(mydata, "%Y")` and you can drop it at the end `select(-year)`. (Or use lubridate like below.) – Gregor Thomas May 12 '15 at 19:15
  • Are your parentheses misplaced? You have a `%>%` inside your `mutate()`! – Frank May 12 '15 at 19:28

2 Answers2

7

If you're up for using the lubridate package you could do:

library("lubridate")

yearsdata %>%
  filter(is.null(years) | year(mydate) %in% years)

Which gives:

#   animal     mydate
# 1    pig 2014-01-01
Kara Woo
  • 3,595
  • 19
  • 31
5

All these pipes gives me headache, I would just do

library(data.table)
setDT(yearsdata)[is.null(years) | year(mydate) %in% years]
#    animal     mydate
# 1:    pig 2014-01-01
David Arenburg
  • 91,361
  • 17
  • 137
  • 196