-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_wrangle.Rmd
75 lines (65 loc) · 2.05 KB
/
1_wrangle.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
output:
html_notebook:
code_folding: show
toc: yes
toc_float: yes
html_document:
df_print: paged
toc: yes
editor_options:
chunk_output_type: inline
---
# Wrangle Data {.tabset}
***
## Bitcoin Price Data
### Wide to long
```{r}
merge <- readRDS("data/bitcoin_raw.rds")
long <- gather(merge, variable, measurement,
Preis:Nachrichtenartikel, factor_key=TRUE)
```
### Drop one day only contained in google data
```{r}
long %>%
filter(date != "2018-02-06") ->
long
```
### Prepare special variable values (min, max, end, quartiles)
```{r}
mins <- group_by(long, variable) %>% slice(which.min(measurement)) # max value ts
maxs <- group_by(long, variable) %>% slice(which.max(measurement)) # min value ts
ends <- group_by(long, variable) %>% filter(date == max(date)) # last value of ts
quarts <- long %>% group_by(variable) %>% # quartiles for grey bar
summarize(quart1 = quantile(measurement, 0.25, na.rm = TRUE),
quart2 = quantile(measurement, 0.75, na.rm = TRUE)) %>%
right_join(long, by = "variable")
```
### Create index values
```{r}
long %>%
filter(complete.cases(.), variable != "close_ratio") %>%
group_by(variable) %>%
mutate(max = max(measurement), index = measurement/max) %>%
filter(variable == "Preis" | variable == "Google-Suchen") ->
long.index
```
## Bitcoin Region
### Prepare for ggplot Map
```{r}
#generate id
data(World)
World@data$id_name <- seq(1,length(World@data$name))
# change spacial data frame to data frame
map_draw <- fortify(World, region = "id_name")
```
### Add World@data
Through the fortify command, some information from the dataset of the spacial data frame ist lost. In this step the World@data part gets merged to the map_draw dataframe by hand.
```{r message=FALSE, warning=FALSE}
map_draw$id <- as.numeric(map_draw$id)
data <- World@data
#Add the stats to the map data
map_draw <- left_join(map_draw, World@data, by = c('id'='id_name')) %>%
left_join(gg_oct17_world, by = c("name" = "Country")) %>%
mutate(bitcoin = ifelse(is.na(Bitcoin), 0, Bitcoin))
```