-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidyr.Rmd
46 lines (40 loc) · 938 Bytes
/
tidyr.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
40
41
42
43
44
45
46
---
title: "tidyr"
author: "Raghu Sidharthan"
output: html_document
---
[RBasic1](RBasic1),
[RBasic2](RBasic2),
[RBasic3](RBasic3),
[data.table](RDTable),
[dplyr](dplyr),
[tidyr](tidyr),
[RGIS](RGIS),
[Leaflet](Leaflet)
```{r,echo=T,warning=FALSE}
library(tidyr)
df <- data.frame(
id = 1:10,
time = as.Date('2009-01-01') + 0:9,
Q3.2.1. = rnorm(10, 0, 1),
Q3.2.2. = rnorm(10, 0, 1),
Q3.2.3. = rnorm(10, 0, 1),
Q3.3.1. = rnorm(10, 0, 1),
Q3.3.2. = rnorm(10, 0, 1),
Q3.3.3. = rnorm(10, 0, 1)
)
df %>%
gather(key, value, -id, -time) %>%
extract(key, c("question", "loop_number"), "(Q.\\..)\\.(.)") %>%
spread(question, value)
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
stocks %>% gather(stock, price, -time)
stocksm <- stocks %>% gather(stock, price, -time)
stocksm %>% spread(stock, price)
stocksm %>% spread(time, price)
```