As usual, let's load the packages and data needed for this practical.
library("dplyr") library("lubridate") library("ggplot2") data(okcupid, package = "jrTidyverse")
Store your birth date as a character variable i.e.
r
bday = "11/04/1967"
Convert it into a date object using dmy
r
bday = dmy(bday)
Which day of the week were you born on? Hint: Use wday()
. Notice R returns the weekday as a number. To clarify this, set the argument label
equal to TRUE
inside wday
.
r
wday(bday, label = TRUE)
Using the year()
function, change the year of your date object to your next birthday. What day is that on?
year(bday) = 2018 wday(bday, label = TRUE)
interval
then use the unit
argument inside as.period()
today = today() as.period(interval(today, bday), unit = "year") as.period(interval(today, bday), unit = "day") as.period(interval(today, bday), unit = "seconds")
Take our OKcupid data, let's say we want to look at the distribution of the weekday of people's last online time. Effectively asking the question "Which day of the week do people use OKCupid most on?"
mutate()
and ymd_hms()
convert the last_online
column to a proper date. Hint, remember to set
the time zone in the ymd_hms()
via tz = "America/Los_Angeles"
.okcupid = okcupid %>% mutate(last_online = ymd_hms(last_online, tz = "America/Los_Angeles"))
week_day
that contains the day of the week a user accessed OKCupid. Hint: use mutate()
and wday()
okcupid = okcupid %>% mutate(week_day = wday(last_online, label = TRUE))
geom_bar()
. Which days are most popular?ggplot(okcupid, aes(x = week_day)) + geom_bar() + xlab("Week day") + ylab("Count")
# friday and saturday are the two most popular
# either use a graph to find out ggplot(okcupid, aes(x = week_day)) + geom_bar() + xlab("Week day") + ylab("Count") + facet_wrap(~sex) # or a summary data frame okcupid %>% group_by(sex) %>% count(week_day)
okcupid = okcupid %>% mutate(lo_hour = hour(last_online))
ggplot(okcupid, aes(x = lo_hour)) + geom_bar() + xlab("Hour of the day") + ylab("Count")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.