-
Notifications
You must be signed in to change notification settings - Fork 3
/
reshape.Rmd
39 lines (25 loc) · 902 Bytes
/
reshape.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
## Reshaping Data
## Reshaping Data
Sometimes you need to reshape your data
- from wide to long
- or vice versa
## Wide to Long
Use the <span class='pack'>tidyr</span> package
<span class='func'>gather</span> will take wide data melt it
- **key** is the new variable name, labels are the original variable names
- **value** is the name of the new variable that will contain their values
```{r wide2long, eval=TRUE, cache=TRUE, echo=-c(1)}
library(tidyr)
bballLong = bball %>%
select(Player, Tm, FG., X3P., X2P., trueShooting, effectiveFG) %>%
rename(fieldGoalPerc = FG., threePointPerc = X3P., twoPointPerc = X2P.) %>%
gather(key = 'vitalInfo', value = 'value', -Tm, -Player)
bballLong %>% head
```
## Long to wide
Going the reverse direction - use <span class="func">spread</span>
```{r long2wide, cache=TRUE, eval=TRUE}
bballLong %>%
spread(vitalInfo, value) %>%
head
```