R-tude: counting my sleep locations
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,721 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,711 more rows
summary(lifetime)
## Date Geo
## Min. :2014-10-04 Length:1721
## 1st Qu.:2015-11-29 Class :character
## Median :2017-02-01 Mode :character
## Mean :2017-01-31
## 3rd Qu.:2018-04-06
## Max. :2019-06-09
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 136
## 4 Philadelphia NA NA NA 5 NA 3
## 5 Ross Dam NA NA NA NA 3 NA
## 6 San Francisco NA NA NA 20 NA 3
## 7 Somerset NA NA NA 13 6 7
## 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.