--- title: "Tidyverse" output: html_notebook --- ## R packages for data science gglot2, dplyr, tidyr, readr, purr, tibble, stringr, forcats "tidyr is new package that makes it easy to “tidy” your data. Tidy data is data that’s easy to work with: it’s easy to munge (with dplyr), visualise (with ggplot2 or ggvis) and model (with R’s hundreds of modelling packages). The two most important properties of tidy data are: Each column is a variable. Each row is an observation."" [Tidyverse](https://www.tidyverse.org/) ```{r} library(tidyr) library(dplyr) messy <- data.frame( name = c("Wilbur", "Petunia", "Gregory"), a = c(67, 80, 64), b = c(56, 90, 50) ) messy ``` ```{r} messy %>% gather(drug, heartrate, a:b) # %>% "chain" operator (takes LHS as argument for RHS) # e.g.: data %>% head() %>% summary() ("chaining") is the same as summary(head(data)) ("nesting") ``` ```{r} set.seed(10) messy <- data.frame( id = 1:4, trt = sample(rep(c('control', 'treatment'), each = 2)), work.T1 = runif(4), home.T1 = runif(4), work.T2 = runif(4), home.T2 = runif(4) ) messy ``` ```{r} tidier <- messy %>% gather(key, time, -id, -trt) tidier %>% head(10) ``` ```{r} tidy <- tidier %>% separate(key, into = c("location", "ttime"), sep = "\\.") tidy %>% head(10) ``` ```{r} back <- tidy %>% spread(location, time) back %>% head(10) ``` similar (more powerful and complex) package: reshape2 with melt and cast ### Links [tidyr](https://blog.rstudio.com/2014/07/22/introducing-tidyr/) [tidyverse](https://www.tidyverse.org/)