Richard Sprague

My personal website

R-tude: counting my sleep locations

2018-09-18


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:

lifetime
## # A tibble: 1,616 x 2
##    Date       Geo  
##    <date>     <chr>
##  1 2014-10-04 <NA> 
##  2 2014-10-05 <NA> 
##  3 2014-10-06 <NA> 
##  4 2014-10-07 <NA> 
##  5 2014-10-08 <NA> 
##  6 2014-10-09 <NA> 
##  7 2014-10-10 <NA> 
##  8 2014-10-11 <NA> 
##  9 2014-10-12 <NA> 
## 10 2014-10-13 <NA> 
## # … with 1,606 more rows
summary(lifetime)
##       Date                Geo           
##  Min.   :2014-10-04   Length:1616       
##  1st Qu.:2015-11-02   Class :character  
##  Median :2016-12-10   Mode  :character  
##  Mean   :2016-12-10                     
##  3rd Qu.:2018-01-17                     
##  Max.   :2019-02-24

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

lifetime %>% tidyr::separate(Date,c("Year","Month","Day")) %>%
  dplyr::group_by(Year) %>% dplyr::count(Geo) %>%
  dplyr::filter(n>2) %>% spread(Year, n)
## # A tibble: 9 x 7
##   Geo           `2014` `2015` `2016` `2017` `2018` `2019`
##   <chr>          <int>  <int>  <int>  <int>  <int>  <int>
## 1 Beijing           NA     NA     NA      6     NA     NA
## 2 Leavenworth       NA     NA     NA     NA      5     NA
## 3 Mercer Island     NA     NA     NA    306    328     52
## 4 Philadelphia      NA     NA     NA      5     NA     NA
## 5 Ross Dam          NA     NA     NA     NA      3     NA
## 6 San Francisco     NA     NA     NA     20     NA     NA
## 7 Somerset          NA     NA     NA     13      6     NA
## 8 Yakima            NA     NA     NA     NA      3     NA
## 9 <NA>              93    370    366     NA     NA     NA

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.