Issue with counting frequency of dates

Greetings,

I am still fairly new to R and still learning the fundamentals. I am using the nycflights13 data built into R and am trying to figure out the number of flights on certain days, i.e. holidays. I have tidy verse and lubridate packages uploaded

So far I have tried this:
nycflights13::flights

year, month, and day are separate so I try to unite them

flights <- nycflights13::flights %>%

  • unite("date", year:day, remove = TRUE)

"date" is created but is not in readable formate for R to subset and I tried I few ways of trying to make it a date but no change with the following in no particular order:

mutate(date = unite(as.Date("year","month", "day")))

as.Date(as.character(date),format="%y/%m/%d")

nycflights13:flights$date <- as.Date(with(nycflights13:flights, paste(year, month, day,sep="-")), "%Y-%m-%d")

Lastly it is worth noting I am able to do this with one date either with subset or filter i.e.:

daytest <- flights %>%

  • subset(date == "2013_1_1")

count(daytest, "date")

But as soon as I try to at a second date, I get an error message, so long story short I seem to have minor issues wit fixing how the date is read.

Your help is appreciated.
Cheers

With the following code you should be able to make a date column and use it for sub setting. The ymd function from lubridate converts text strings in the ymd format to a date.

flights <- nycflights13::flights %>% 
  unite("date", year:day) %>% 
  mutate(date = ymd(date))
date_test <- subset(flights, date == ymd("2013-01-02"))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

I cannot test this code at the moment but I think you want to create the subset of data with the following code.

date_test <- subset(flights, date %in% ymd(c("2013-01-02","2013-04-03","2013-05-12")))

Notice that I used c() to make a vector of the dates and I used the %in% operator to check if the date value is a member of that vector.

Thanks for your help. This helped with converting the character date to 'date' recognition. One follow-up though with a small issue to subsetting, below for examples:

#run provided code:
flights <- nycflights13::flights %>%
unite("date", year:day) %>%
mutate(date = ymd(date))
date_test <- subset(flights, date == ymd("2013-01-02"))
#count frequency of flights on that day
count(date_test, "date")

A tibble: 1 x 2

"date" n

1 date 943

Add second date

lights <- nycflights13::flights %>%

  • unite("date", year:day) %>% 
    
  • mutate(date = ymd(date))
    

date_test <- subset(flights, date == ymd("2013-01-02", "2013-04-03"))
count(date_test, "date")

A tibble: 1 x 2

"date" n

1 date 968

add third date

flights <- nycflights13::flights %>%

  • unite("date", year:day) %>%
  • mutate(date = ymd(date))

date_test <- subset(flights, date == ymd("2013-01-02","2013-04-03","2013-05-12"))
Warning message:
In ==.default(date, ymd("2013-01-02", "2013-04-03", "2013-05-12")) :
longer object length is not a multiple of shorter object length
count(date_test, date)

A tibble: 3 x 2

date n

1 2013-01-02 314
2 2013-04-03 331
3 2013-05-12 299

Your clarification on this is highly appreciated.
Cheers

Perfect! Thanks for the help