We aren’t computer scientists and that’s okay!
We make lots of mistakes. Mistakes are funny. You can laugh with us.
Let’s go, Simba, Pumbaa, and Timon!
It’s about time! Lubridate makes working with dates much easier.
We can find how much time has elapsed, add or subtract days, and find seasonal and day of a week averages.
#Conver Month-Day-Year
x <- "08-18-2020" # 08/18/2020
x_date <- mdy(x)
#Conver Day-Month-Year
x <- "18-09-2020"
x_date <- dmy(x)
#convert Year-Month-day, guess which function to use?
# convert Year-Month-day Hour:Minutes
x <- "2020-08-24 8:35 AM"
x_date <- ymd_hm(x)
# convert Year-Month-day Hour:Minutes:Seconds
x <- "2020-08-24 8:35:22 AM"
x_date <- ymd_hms(x)
Sometimes we’ll have years, months, days speaded across multiple columns. We can use make_date or make_datetime to create date or datetime.
library(tidyverse)
library(janitor)
aq <- airquality %>%
mutate(year = 1973) %>%
clean_names()%>% # call clean_names from the janitor package
rename(solar = solar_r) # use rename function to modify the column name
head(aq)
aq <- aq %>%
mutate(date = make_date(year, month, day))
# with the date column, we can visualize ozone concentration across May - Sep
aq %>%
ggplot(aes(x=date, y=ozone)) +
geom_point() +
geom_line()
left_join(scrap, convert, by = c(“columns to join by”))
left_join() works like a zipper to combine 2 tables based on one or more variables. It’s called “left”-join because the entire table on the left side is retained.
Anything that matches from the right table gets to join the party, but any rows that don’t have a matching ID will be ignored.
Remember our porg friends? We haven’t share their names.
The joined result:
right_join(): includes all rows in right table.
inner_join(): includes all rows in both tables.
full_join(): includes all rows in left or right table.
band_members %>%
inner_join(band_instruments, by = "name")
band_members %>%
full_join(band_instruments, by = "name")
Exercise
Install and load the nycflights13 package.
Create a table that includes all flights and weather of their destination airports.
date-time
join tables