-
Notifications
You must be signed in to change notification settings - Fork 6
/
rbasic.Rmd
185 lines (146 loc) · 4.69 KB
/
rbasic.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "rbasic"
author: "知了新聞團隊"
# date: "3/19/2017"
date: "`r Sys.Date()`"
output:
ioslides_presentation:
incremental: true
---
```{r setup 1, include=FALSE}
knitr::opts_chunk$set(echo = TRUE )
```
## 回頭來看 R 基礎語法 {.build}
- 向量式的運算
```{r vectorize}
c(1, 2, 3, 4) + 1
c(1, 2, 3, 4) + c(2, 3, 4, 5)
c(1, 2, 3, 4) + c(2, 10)
```
- %&@$^*%@!
- 「阿鬼你還是說中文吧!」
## 回頭來看 R 基礎語法 {.build}
*To understand computations in R, two slogans are helpful:*
- *Everything that exists is an object.*
- *Everything that happens is a function call."*
*— John Chambers*
<div class="columns-2">
```{r function call, warning=FALSE, message=FALSE}
`+`
`<-`
`[`
`c`
```
</div>
## 回頭來看 R 基礎語法 {.build}
- 什麼是 `function call`?
```{r function call 1}
1 + 1
`+`(1, 1)
```
## 回頭來看 R 基礎語法 {.build}
- 沒關係不重要,搞懂 `Dataframe` 就好
- (直翻)資料框,就是表格的意思啦
- 看起來大概差不多就是長這種樣子(?)
```{r dataframe 0, echo=FALSE, warning=FALSE, message=FALSE}
# install.packages("Lahman")
library(Lahman)
library(dplyr)
```
```{r dataframe 1}
wang <- Pitching %>% filter(playerID == "wangch01") %>% arrange(desc(yearID))
wang
```
## 新潮好用的玩意兒 R {.build}
- 好用工具`懶人包` 🎒👝👛👜💼
- [`tidyverse`](http://tidyverse.org/)
![](http://forcats.tidyverse.org/logo.png)
![](http://ggplot2.tidyverse.org/logo.png)
![](http://haven.tidyverse.org/logo.png)
![](http://readr.tidyverse.org/logo.png)
![](http://stringr.tidyverse.org/logo.png)
![](http://tidyr.tidyverse.org/logo.png)
```{r tidyverse}
# install.packages("tidyverse")
library(tidyverse)
```
## 畫圖之前先把資料讀進來 {.build}
- `readr::read_csv()`
- 跟原生方法比起來,效率較高,也不會將文字格式轉成 `factor`(因子)來處理
- 比 `data.table::fread()` 略慢,不過語法較簡單
```{r read_csv 0}
traffic <- read_csv("ggplot2-slides/traffic_eng.csv")
```
## 畫圖之前先把資料讀進來 {.build}
- `readr::read_csv()`
```{r read_csv 1}
head(traffic, n=3)
```
## 畫圖之前先把資料讀進來 {.build}
- `readr::read_csv()`
```{r read_csv 2}
traffic <- read_csv("ggplot2-slides/traffic_eng.csv", col_types =
cols(
time.year = col_integer(),
time.month = col_integer(),
time.day = col_integer(),
time.hour = col_integer(),
time.minute = col_integer(),
event.level = col_character(),
location.district = col_character(),
location.address = col_character(),
number.dead = col_integer(),
number.injury = col_integer(),
party.sn = col_integer(),
vehicle.type = col_character(),
party.gender = col_character(),
party.age = col_integer(),
party.injury = col_character(),
location.weather = col_character(),
location.speed.limit = col_integer(),
location.road.type = col_character(),
location.type = col_character()
))
```
## 畫圖之前先把資料讀進來 {.build}
- `readr::read_csv()`
```{r read_csv 3}
head(traffic, n=3)
```
## 畫圖之前先把資料讀進來 {.build}
- `dplyr::data_frame()`
- 跟原生方法比起來,效率較高,也不會將文字格式轉成 `factor`(因子)來處理
- 比 `data.table::data.table()` 略慢,不過語法較簡單
```{r data_frame}
traffic <- data_frame(time.year = c(104), time.month = c(1),
time.day = c(1), time.hour = c(0),
time.minute = c(0, 1, 2), event.level = c("一般"))
head(traffic, n=3)
```
## 畫圖之前先把資料讀進來 {.build}
- `magrittr` (pipe `%>%`)
- 把工作流程的每一站`串接`起來,簡化開發邏輯跟程式碼
```{r pipe 0}
head(traffic, n=3)
```
## 畫圖之前先把資料讀進來 {.build}
- `magrittr` (pipe `%>%`)
- 把工作流程的每一站`串接`起來,簡化開發邏輯跟程式碼
```{r pipe 1}
traffic %>% head(3)
```
## 畫圖之前先把資料讀進來 {.build}
- `dplyr::select()`
```{r dplyr 0}
wang %>% select(yearID, teamID, W, L, ERA)
```
## 畫圖之前先把資料讀進來 {.build}
- `dplyr::filter()`
```{r dplyr 1}
wang %>% filter(GS > 10)
```
## 畫圖之前先把資料讀進來 {.build}
- `dplyr::summarise()`
```{r dplyr 2}
wang %>% group_by(lgID) %>% summarise(mean(ERA))
```