R-tude: counting my sleep locations

R
Published

September 18, 2018

An etude is a short musical composition intended for intensive practice on a particular skill. My R-tudes are similar exercises to help me develop my R skills.

This is an R-tude using tidyverse and various summarization functions to help me quickly generate a chart showing where I have slept over the past few years. It’s based on an excel spreadsheet that I read into the variable lifetime, which is a simple dataframe made of two columns: Date and Geo, which tell the geographical location where I slept on a given night.

In tibble form, it looks like this:

Code
lifetime
summary(lifetime)

Here’s how I summarize the number of nights per year, by location

Code
lifetime %>% tidyr::separate(Date,c("Year","Month","Day")) %>%
  dplyr::group_by(Year) %>% dplyr::count(Geo) %>%
  dplyr::filter(n>2) %>% spread(Year, n)

There you go! This one line of R prose gives me another dataframe organized by year, showing only the locations where I slept for at least 2 nights.