5 種 data types:
- 向量 (vector)
- factor
- list
- data.frame
- matrix
3 種 element types:
- 數值: 實數 double, 整數 integer
- 字串: ” ”
- 邏輯值: TRUE, FALSE, 邏輯表達式 <, >, <=, >=, ==, !,
&, | 的結果
邏輯運算
m = mtcars$mpg
s = m |> summary()
m[m < s[2]]
[1] 14.3 15.2 10.4 10.4 14.7 15.2 13.3 15.0
[1] 22.8 24.4 22.8 32.4 30.4 33.9 27.3 26.0 30.4
[1] 22.8 14.3 24.4 22.8 15.2 10.4 10.4 14.7 32.4 30.4 33.9 15.2 13.3 27.3 26.0
[16] 30.4 15.0
[1] 21.0 21.0 21.4 18.7 18.1 19.2 17.8 16.4 17.3 21.5 15.5 19.2 15.8 19.7 21.4
[1] 13.5625
[1] 27.3
m[m < s[2] | m >= s[5]] |> min()
[1] 10.4
m[m < s[2] | m >= s[5]] |> max()
[1] 33.9
[1] 10.4
[1] 33.9
m[m < s[5] & m >= s[2]] |> median()
[1] 19.2
列的操作
- mtcars 的倒數第 12 列
- 1 到 173 的前 1/4 與後 1/4 各包含哪些數字
- flights 的前 1/4 列的最後一列
- flights 的後 1/4 列的第一列
mtcars |> tail(12) |> head(1)
n = 1:173 |> summary()
1:n[2]
[1] 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] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
[20] 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
[39] 168 169 170 171 172 173
n1 = 1:nrow(flights) |> summary()
n1[2]*2
1st Qu.
168389.5
flights[as.integer(n1[2]),]
flights[as.integer(n1[5]),]
部分樣本的分組直方圖
- mpg 資料檔中 manufacturer 樣本最多的 3 家名稱 n
- 挑出這 3 家的樣本,製作
mpg$manufacturer |> table()
audi chevrolet dodge ford honda hyundai jeep
18 19 37 25 9 14 8
land rover lincoln mercury nissan pontiac subaru toyota
4 3 4 13 5 14 34
volkswagen
27
a = mpg$manufacturer |> table() |> sort()
n = a |> tail(3) |> names()
m = mpg[mpg$manufacturer %in% n,]
barplot(table(m$class,m$manufacturer),beside = T)

m |> ggplot() +
geom_bar(aes(manufacturer, fill = class), position = "dodge")

分組統計量及直方圖
f = flights[flights$hour >= 5,]
tapply(f$arr_time,f$hour,median,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

tapply(flights$distance,flights$origin,median,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

flights$carrier |> table()
9E AA AS B6 DL EV F9 FL HA MQ OO UA US
18460 32729 714 54635 48110 54173 685 3260 342 26397 32 58665 20536
VX WN YV
5162 12275 601
tapply(flights$arr_delay,flights$carrier,mean,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

tapply(airquality$Temp,airquality$Month,mean) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

tapply(airquality$Ozone,airquality$Month,mean,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

tapply(airquality$Solar.R,airquality$Month,mean,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

tapply(airquality$Wind,airquality$Month,mean,na.rm = T) |>
enframe() |>
ggplot() + geom_col(aes(name,value))

airquality |> ggplot() + geom_point(aes(Month,Temp))

airquality |> ggplot() + geom_point(aes(Month,Wind))

airquality |> na.omit() |>
ggplot() + geom_point(aes(Month,Ozone))

airquality |> na.omit() |>
ggplot() + geom_point(aes(Month,Solar.R))

特殊向量:
[1] 11 12 13 14 15
[1] 11 14 17 20 23
seq(11,15, length.out = 7)
[1] 11.00000 11.66667 12.33333 13.00000 13.66667 14.33333 15.00000
[1] 11.00000 11.66667 12.33333 13.00000 13.66667 14.33333 15.00000
x = 1:3
rep(x, times = 3)
[1] 1 2 3 1 2 3 1 2 3
[1] 1 2 3 1 2 3 1 2 3
[1] 1 1 1 1 2 2 2 2 3 3 3 3
[1] 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3
[1] 1 2 3 1 2 3 1
[1] 1 1 1 1 2 2 3 3 3 3 3 3