#library(gt)
data (mpg)
mpg %>%
select (manufacturer: cyl) %>%
head () #查看前6行
# A tibble: 6 × 5
manufacturer model displ year cyl
<chr> <chr> <dbl> <int> <int>
1 audi a4 1.8 1999 4
2 audi a4 1.8 1999 4
3 audi a4 2 2008 4
4 audi a4 2 2008 4
5 audi a4 2.8 1999 6
6 audi a4 2.8 1999 6
mpg %>%
select (1 : 5 ) %>%
head ()
# A tibble: 6 × 5
manufacturer model displ year cyl
<chr> <chr> <dbl> <int> <int>
1 audi a4 1.8 1999 4
2 audi a4 1.8 1999 4
3 audi a4 2 2008 4
4 audi a4 2 2008 4
5 audi a4 2.8 1999 6
6 audi a4 2.8 1999 6
mpg %>%
select (4 ,5 ,7 : 9 ) %>%
head ()
# A tibble: 6 × 5
year cyl drv cty hwy
<int> <int> <chr> <int> <int>
1 1999 4 f 18 29
2 1999 4 f 21 29
3 2008 4 f 20 31
4 2008 4 f 21 30
5 1999 6 f 16 26
6 1999 6 f 18 26
mpg %>%
select (year,cyl,drv: hwy) %>%
head ()
# A tibble: 6 × 5
year cyl drv cty hwy
<int> <int> <chr> <int> <int>
1 1999 4 f 18 29
2 1999 4 f 21 29
3 2008 4 f 20 31
4 2008 4 f 21 30
5 1999 6 f 16 26
6 1999 6 f 18 26
1.2 用mutate()函数在数据框中追加新的变量,将油耗变量cty和hwy(miles per gallon)转换成转公里/升(kilometers per liter)的油耗指标。(1 miles per gallon = 0.425 kilometers per liter)
mpgnew <- mpg %>%
mutate (transmission =
if_else (substring (trans, 1 ,4 ) == "auto" ,
"auto" ,"manual" ))
head (mpgnew) %>% gt ()
audi
a4
1.8
1999
4
auto(l5)
f
18
29
p
compact
12.325
7.650
auto
audi
a4
1.8
1999
4
manual(m5)
f
21
29
p
compact
12.325
8.925
manual
audi
a4
2.0
2008
4
manual(m6)
f
20
31
p
compact
13.175
8.500
manual
audi
a4
2.0
2008
4
auto(av)
f
21
30
p
compact
12.750
8.925
auto
audi
a4
2.8
1999
6
auto(l5)
f
16
26
p
compact
11.050
6.800
auto
audi
a4
2.8
1999
6
manual(m5)
f
18
26
p
compact
11.050
7.650
manual
#二值转换 if_else
mpg$ transmission <- if_else (
substring (mpg$ trans, 1 ,4 ) == "auto" ,
"auto" ,"manual" )
#多值转换 case_when
mpg$ drive <- case_when (
mpg$ drv == "f" ~ "front-wheel" ,
mpg$ drv == "r" ~ "rear-wheel" ,
mpg$ drv == "4" ~ "four-wheel" )
head (mpg) %>% gt ()
audi
a4
1.8
1999
4
auto(l5)
f
18
29
p
compact
12.325
7.650
auto
front-wheel
audi
a4
1.8
1999
4
manual(m5)
f
21
29
p
compact
12.325
8.925
manual
front-wheel
audi
a4
2.0
2008
4
manual(m6)
f
20
31
p
compact
13.175
8.500
manual
front-wheel
audi
a4
2.0
2008
4
auto(av)
f
21
30
p
compact
12.750
8.925
auto
front-wheel
audi
a4
2.8
1999
6
auto(l5)
f
16
26
p
compact
11.050
6.800
auto
front-wheel
audi
a4
2.8
1999
6
manual(m5)
f
18
26
p
compact
11.050
7.650
manual
front-wheel
# the & operator is used to combine two conditions, where both conditions must be true for a row to be included in the filtered data frame.
mpg %>%
filter (class == "suv" &
cyl == 8 &
year == 2008 ) %>%
head () %>%
gt ()
chevrolet
c1500 suburban 2wd
5.3
2008
8
auto(l4)
r
14
20
r
suv
8.500
5.950
auto
rear-wheel
chevrolet
c1500 suburban 2wd
5.3
2008
8
auto(l4)
r
11
15
e
suv
6.375
4.675
auto
rear-wheel
chevrolet
c1500 suburban 2wd
5.3
2008
8
auto(l4)
r
14
20
r
suv
8.500
5.950
auto
rear-wheel
chevrolet
c1500 suburban 2wd
6.0
2008
8
auto(l4)
r
12
17
r
suv
7.225
5.100
auto
rear-wheel
chevrolet
k1500 tahoe 4wd
5.3
2008
8
auto(l4)
4
14
19
r
suv
8.075
5.950
auto
four-wheel
chevrolet
k1500 tahoe 4wd
5.3
2008
8
auto(l4)
4
11
14
e
suv
5.950
4.675
auto
four-wheel
mpg %>%
filter (substr (trans, 1 , 4 ) == "auto" &
class %in% c ("compact" , "subcompact" ) &
! fl %in% c ("c" ,"d" ,"e" )) %>%
head () %>%
gt ()
audi
a4
1.8
1999
4
auto(l5)
f
18
29
p
compact
12.325
7.650
auto
front-wheel
audi
a4
2.0
2008
4
auto(av)
f
21
30
p
compact
12.750
8.925
auto
front-wheel
audi
a4
2.8
1999
6
auto(l5)
f
16
26
p
compact
11.050
6.800
auto
front-wheel
audi
a4
3.1
2008
6
auto(av)
f
18
27
p
compact
11.475
7.650
auto
front-wheel
audi
a4 quattro
1.8
1999
4
auto(l5)
4
16
25
p
compact
10.625
6.800
auto
four-wheel
audi
a4 quattro
2.0
2008
4
auto(s6)
4
19
27
p
compact
11.475
8.075
auto
four-wheel
# the | operator combines the two conditions, where only one of the conditions needs to be true for a row to be included in the filtered data frame.
mpg %>%
filter (class == "suv" |
cyl == 8 |
year == 2008 ) %>%
head () %>%
gt ()
audi
a4
2.0
2008
4
manual(m6)
f
20
31
p
compact
13.175
8.500
manual
front-wheel
audi
a4
2.0
2008
4
auto(av)
f
21
30
p
compact
12.750
8.925
auto
front-wheel
audi
a4
3.1
2008
6
auto(av)
f
18
27
p
compact
11.475
7.650
auto
front-wheel
audi
a4 quattro
2.0
2008
4
manual(m6)
4
20
28
p
compact
11.900
8.500
manual
four-wheel
audi
a4 quattro
2.0
2008
4
auto(s6)
4
19
27
p
compact
11.475
8.075
auto
four-wheel
audi
a4 quattro
3.1
2008
6
auto(s6)
4
17
25
p
compact
10.625
7.225
auto
four-wheel