https://github.com/trinker/tidyr_in_a_nutshell
https://github.com/trinker/tidyr_in_a_nutshell
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/trinker/tidyr_in_a_nutshell
- Owner: trinker
- Created: 2014-10-25T02:03:57.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-10-25T14:02:55.000Z (over 11 years ago)
- Last Synced: 2025-02-14T12:41:04.592Z (over 1 year ago)
- Size: 148 KB
- Stars: 18
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
tidyr In a Nutshell
===
This is a minimal guide, mostly for myself, to remind me of the most import tidyr functions and how they relate to **reshape2** functions that I'm familiar with. Also checkout [dplyr In a Nutshell](https://github.com/trinker/dplyr_in_a_nutshell).
```{r setup, include=FALSE, echo=FALSE}
library(knitr)
opts_chunk$set(comment=NA, tidy=FALSE, message = FALSE, warnings = FALSE)
```
# 2 Main Functions
List of **tidyr** functions and the **reshape2** functions they're related to:
reshape2 Function | tidyr Function | Special Powers
---------------------|-------------------|----------------------------
`melt` | `gather` | long format\*
`dcast` | `spread` | wide format\*
\*[Hadley notes](http://vita.had.co.nz/papers/tidy-data.pdf) these terms are imprecise but good enough for my little noodle
## Arguments (when chaining)

# Demos
### Some Data
```{r}
library(tidyr); library(dplyr)
dat <- data.frame(
id = LETTERS[1:5],
x = sample(0:1, 5, TRUE),
y = sample(0:1, 5, TRUE),
z = sample(0:1, 5, TRUE)
)
dat
```
### gather in Action
```{r}
dat %>% gather(item, scores, -c(id))
```
### spread it Back Out
```{r}
dat %>% gather(item, scores, -c(id)) %>%
spread(item, scores)
```
---
# Additional Demos
### gather
```{r}
dat %>% gather(item, scores, x:z)
dat %>% gather(item, scores, x, y, z)
```
### spread
```{r}
dat %>% gather(item, scores, -c(id)) %>%
spread(id, scores)
```
```{r, echo=FALSE, eval=FALSE}
## knitr::knit2html("README.Rmd")
```