# A tibble: 53,940 × 9
carat cut color clarity table price x y z
<dbl> <ord> <ord> <ord> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 65 327 4.05 4.07 2.31
4 0.29 Premium I VS2 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 57 336 3.94 3.96 2.48
7 0.24 Very Good I VVS1 57 336 3.95 3.98 2.47
8 0.26 Very Good H SI1 55 337 4.07 4.11 2.53
9 0.22 Fair E VS2 61 337 3.87 3.78 2.49
10 0.23 Very Good H VS1 61 338 4 4.05 2.39
# ℹ 53,930 more rows
tidyselect 也可以根据名称选取列。
diamonds %>%select(cut) # 选取cut变量
# A tibble: 53,940 × 1
cut
<ord>
1 Ideal
2 Premium
3 Good
4 Premium
5 Good
6 Very Good
7 Very Good
8 Very Good
9 Fair
10 Very Good
# ℹ 53,930 more rows
diamonds %>%select(-cut) # 删除cut变量
# A tibble: 53,940 × 9
carat color clarity depth table price x y z
<dbl> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 E VS1 56.9 65 327 4.05 4.07 2.31
4 0.29 I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 J VVS2 62.8 57 336 3.94 3.96 2.48
7 0.24 I VVS1 62.3 57 336 3.95 3.98 2.47
8 0.26 H SI1 61.9 55 337 4.07 4.11 2.53
9 0.22 E VS2 65.1 61 337 3.87 3.78 2.49
10 0.23 H VS1 59.4 61 338 4 4.05 2.39
# ℹ 53,930 more rows
6.2: 和 c()
: 表示多个连续的整数,比如 1:3 表示 1~3 这三个整数。
1:5
[1] 1 2 3 4 5
# 选取前五个变量diamonds %>%select(1:5)
# A tibble: 53,940 × 5
carat cut color clarity depth
<dbl> <ord> <ord> <ord> <dbl>
1 0.23 Ideal E SI2 61.5
2 0.21 Premium E SI1 59.8
3 0.23 Good E VS1 56.9
4 0.29 Premium I VS2 62.4
5 0.31 Good J SI2 63.3
6 0.24 Very Good J VVS2 62.8
7 0.24 Very Good I VVS1 62.3
8 0.26 Very Good H SI1 61.9
9 0.22 Fair E VS2 65.1
10 0.23 Very Good H VS1 59.4
# ℹ 53,930 more rows
# 用另一种方式选取前五个变量diamonds %>%select(carat:depth)
# A tibble: 53,940 × 5
carat cut color clarity depth
<dbl> <ord> <ord> <ord> <dbl>
1 0.23 Ideal E SI2 61.5
2 0.21 Premium E SI1 59.8
3 0.23 Good E VS1 56.9
4 0.29 Premium I VS2 62.4
5 0.31 Good J SI2 63.3
6 0.24 Very Good J VVS2 62.8
7 0.24 Very Good I VVS1 62.3
8 0.26 Very Good H SI1 61.9
9 0.22 Fair E VS2 65.1
10 0.23 Very Good H VS1 59.4
# ℹ 53,930 more rows
# A tibble: 53,940 × 4
carat color clarity depth
<dbl> <ord> <ord> <dbl>
1 0.23 E SI2 61.5
2 0.21 E SI1 59.8
3 0.23 E VS1 56.9
4 0.29 I VS2 62.4
5 0.31 J SI2 63.3
6 0.24 J VVS2 62.8
7 0.24 I VVS1 62.3
8 0.26 H SI1 61.9
9 0.22 E VS2 65.1
10 0.23 H VS1 59.4
# ℹ 53,930 more rows
6.3starts_with() 系列函数
# 选取c开头的变量diamonds %>%select(starts_with("c"))
# A tibble: 53,940 × 4
carat cut color clarity
<dbl> <ord> <ord> <ord>
1 0.23 Ideal E SI2
2 0.21 Premium E SI1
3 0.23 Good E VS1
4 0.29 Premium I VS2
5 0.31 Good J SI2
6 0.24 Very Good J VVS2
7 0.24 Very Good I VVS1
8 0.26 Very Good H SI1
9 0.22 Fair E VS2
10 0.23 Very Good H VS1
# ℹ 53,930 more rows
# A tibble: 53,940 × 3
cut color clarity
<ord> <ord> <ord>
1 Ideal E SI2
2 Premium E SI1
3 Good E VS1
4 Premium I VS2
5 Good J SI2
6 Very Good J VVS2
7 Very Good I VVS1
8 Very Good H SI1
9 Fair E VS2
10 Very Good H VS1
# ℹ 53,930 more rows