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:
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.
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)