第1题
- 用select()函数从mpg提取5个变量⽣成新的数据框。
#library(tidyverse)
#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
第3题
- 任选mpg中的某个变量,用if_else()函数对该变量的数值进⾏条件转换。
mpgnew <- mpg %>%
mutate(transmission =
if_else(substring(trans, 1,4) == "auto",
"auto","manual"))
head(mpgnew) %>% gt()
manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
hwy.kpl |
cty.kpl |
transmission |
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()
manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
hwy.kpl |
cty.kpl |
transmission |
drive |
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 |
第4题
- 设置三个筛选条件,用filter()函数从mpg筛选个案⽣成新的数据框。
# 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) %>%
gt()
manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
hwy.kpl |
cty.kpl |
transmission |
drive |
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 |
dodge |
durango 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
13 |
17 |
r |
suv |
7.225 |
5.525 |
auto |
four-wheel |
dodge |
durango 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
9 |
12 |
e |
suv |
5.100 |
3.825 |
auto |
four-wheel |
dodge |
durango 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
13 |
17 |
r |
suv |
7.225 |
5.525 |
auto |
four-wheel |
dodge |
durango 4wd |
5.7 |
2008 |
8 |
auto(l5) |
4 |
13 |
18 |
r |
suv |
7.650 |
5.525 |
auto |
four-wheel |
ford |
expedition 2wd |
5.4 |
2008 |
8 |
auto(l6) |
r |
12 |
18 |
r |
suv |
7.650 |
5.100 |
auto |
rear-wheel |
ford |
explorer 4wd |
4.6 |
2008 |
8 |
auto(l6) |
4 |
13 |
19 |
r |
suv |
8.075 |
5.525 |
auto |
four-wheel |
jeep |
grand cherokee 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
9 |
12 |
e |
suv |
5.100 |
3.825 |
auto |
four-wheel |
jeep |
grand cherokee 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
14 |
19 |
r |
suv |
8.075 |
5.950 |
auto |
four-wheel |
jeep |
grand cherokee 4wd |
5.7 |
2008 |
8 |
auto(l5) |
4 |
13 |
18 |
r |
suv |
7.650 |
5.525 |
auto |
four-wheel |
jeep |
grand cherokee 4wd |
6.1 |
2008 |
8 |
auto(l5) |
4 |
11 |
14 |
p |
suv |
5.950 |
4.675 |
auto |
four-wheel |
land rover |
range rover |
4.2 |
2008 |
8 |
auto(s6) |
4 |
12 |
18 |
r |
suv |
7.650 |
5.100 |
auto |
four-wheel |
land rover |
range rover |
4.4 |
2008 |
8 |
auto(s6) |
4 |
12 |
18 |
r |
suv |
7.650 |
5.100 |
auto |
four-wheel |
lincoln |
navigator 2wd |
5.4 |
2008 |
8 |
auto(l6) |
r |
12 |
18 |
r |
suv |
7.650 |
5.100 |
auto |
rear-wheel |
mercury |
mountaineer 4wd |
4.6 |
2008 |
8 |
auto(l6) |
4 |
13 |
19 |
r |
suv |
8.075 |
5.525 |
auto |
four-wheel |
nissan |
pathfinder 4wd |
5.6 |
2008 |
8 |
auto(s5) |
4 |
12 |
18 |
p |
suv |
7.650 |
5.100 |
auto |
four-wheel |
toyota |
4runner 4wd |
4.7 |
2008 |
8 |
auto(l5) |
4 |
14 |
17 |
r |
suv |
7.225 |
5.950 |
auto |
four-wheel |
toyota |
land cruiser wagon 4wd |
5.7 |
2008 |
8 |
auto(s6) |
4 |
13 |
18 |
r |
suv |
7.650 |
5.525 |
auto |
four-wheel |
mpg %>%
filter(substr(trans, 1, 4) == "auto" &
class %in% c("compact", "subcompact") &
!fl %in% c("c" ,"d" ,"e")) %>%
gt()
manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
hwy.kpl |
cty.kpl |
transmission |
drive |
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 |
audi |
a4 quattro |
2.8 |
1999 |
6 |
auto(l5) |
4 |
15 |
25 |
p |
compact |
10.625 |
6.375 |
auto |
four-wheel |
audi |
a4 quattro |
3.1 |
2008 |
6 |
auto(s6) |
4 |
17 |
25 |
p |
compact |
10.625 |
7.225 |
auto |
four-wheel |
ford |
mustang |
3.8 |
1999 |
6 |
auto(l4) |
r |
18 |
25 |
r |
subcompact |
10.625 |
7.650 |
auto |
rear-wheel |
ford |
mustang |
4.0 |
2008 |
6 |
auto(l5) |
r |
16 |
24 |
r |
subcompact |
10.200 |
6.800 |
auto |
rear-wheel |
ford |
mustang |
4.6 |
1999 |
8 |
auto(l4) |
r |
15 |
21 |
r |
subcompact |
8.925 |
6.375 |
auto |
rear-wheel |
ford |
mustang |
4.6 |
2008 |
8 |
auto(l5) |
r |
15 |
22 |
r |
subcompact |
9.350 |
6.375 |
auto |
rear-wheel |
honda |
civic |
1.6 |
1999 |
4 |
auto(l4) |
f |
24 |
32 |
r |
subcompact |
13.600 |
10.200 |
auto |
front-wheel |
honda |
civic |
1.6 |
1999 |
4 |
auto(l4) |
f |
24 |
32 |
r |
subcompact |
13.600 |
10.200 |
auto |
front-wheel |
honda |
civic |
1.8 |
2008 |
4 |
auto(l5) |
f |
25 |
36 |
r |
subcompact |
15.300 |
10.625 |
auto |
front-wheel |
hyundai |
tiburon |
2.0 |
1999 |
4 |
auto(l4) |
f |
19 |
26 |
r |
subcompact |
11.050 |
8.075 |
auto |
front-wheel |
hyundai |
tiburon |
2.0 |
2008 |
4 |
auto(l4) |
f |
20 |
27 |
r |
subcompact |
11.475 |
8.500 |
auto |
front-wheel |
hyundai |
tiburon |
2.7 |
2008 |
6 |
auto(l4) |
f |
17 |
24 |
r |
subcompact |
10.200 |
7.225 |
auto |
front-wheel |
nissan |
altima |
2.4 |
1999 |
4 |
auto(l4) |
f |
19 |
27 |
r |
compact |
11.475 |
8.075 |
auto |
front-wheel |
subaru |
impreza awd |
2.2 |
1999 |
4 |
auto(l4) |
4 |
21 |
26 |
r |
subcompact |
11.050 |
8.925 |
auto |
four-wheel |
subaru |
impreza awd |
2.5 |
1999 |
4 |
auto(l4) |
4 |
19 |
26 |
r |
subcompact |
11.050 |
8.075 |
auto |
four-wheel |
subaru |
impreza awd |
2.5 |
2008 |
4 |
auto(s4) |
4 |
20 |
25 |
p |
compact |
10.625 |
8.500 |
auto |
four-wheel |
subaru |
impreza awd |
2.5 |
2008 |
4 |
auto(s4) |
4 |
20 |
27 |
r |
compact |
11.475 |
8.500 |
auto |
four-wheel |
toyota |
camry solara |
2.2 |
1999 |
4 |
auto(l4) |
f |
21 |
27 |
r |
compact |
11.475 |
8.925 |
auto |
front-wheel |
toyota |
camry solara |
2.4 |
2008 |
4 |
auto(s5) |
f |
22 |
31 |
r |
compact |
13.175 |
9.350 |
auto |
front-wheel |
toyota |
camry solara |
3.0 |
1999 |
6 |
auto(l4) |
f |
18 |
26 |
r |
compact |
11.050 |
7.650 |
auto |
front-wheel |
toyota |
camry solara |
3.3 |
2008 |
6 |
auto(s5) |
f |
18 |
27 |
r |
compact |
11.475 |
7.650 |
auto |
front-wheel |
toyota |
corolla |
1.8 |
1999 |
4 |
auto(l3) |
f |
24 |
30 |
r |
compact |
12.750 |
10.200 |
auto |
front-wheel |
toyota |
corolla |
1.8 |
1999 |
4 |
auto(l4) |
f |
24 |
33 |
r |
compact |
14.025 |
10.200 |
auto |
front-wheel |
toyota |
corolla |
1.8 |
2008 |
4 |
auto(l4) |
f |
26 |
35 |
r |
compact |
14.875 |
11.050 |
auto |
front-wheel |
volkswagen |
gti |
2.0 |
1999 |
4 |
auto(l4) |
f |
19 |
26 |
r |
compact |
11.050 |
8.075 |
auto |
front-wheel |
volkswagen |
gti |
2.0 |
2008 |
4 |
auto(s6) |
f |
22 |
29 |
p |
compact |
12.325 |
9.350 |
auto |
front-wheel |
volkswagen |
jetta |
2.0 |
1999 |
4 |
auto(l4) |
f |
19 |
26 |
r |
compact |
11.050 |
8.075 |
auto |
front-wheel |
volkswagen |
jetta |
2.0 |
2008 |
4 |
auto(s6) |
f |
22 |
29 |
p |
compact |
12.325 |
9.350 |
auto |
front-wheel |
volkswagen |
jetta |
2.5 |
2008 |
5 |
auto(s6) |
f |
21 |
29 |
r |
compact |
12.325 |
8.925 |
auto |
front-wheel |
volkswagen |
jetta |
2.8 |
1999 |
6 |
auto(l4) |
f |
16 |
23 |
r |
compact |
9.775 |
6.800 |
auto |
front-wheel |
volkswagen |
new beetle |
2.0 |
1999 |
4 |
auto(l4) |
f |
19 |
26 |
r |
subcompact |
11.050 |
8.075 |
auto |
front-wheel |
volkswagen |
new beetle |
2.5 |
2008 |
5 |
auto(s6) |
f |
20 |
29 |
r |
subcompact |
12.325 |
8.500 |
auto |
front-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()
manufacturer |
model |
displ |
year |
cyl |
trans |
drv |
cty |
hwy |
fl |
class |
hwy.kpl |
cty.kpl |
transmission |
drive |
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 |