Skip to main content

列の選択

準備

1. パッケージのインストール

install.packages("tidyverse")

2. パッケージの読み込み

library(tidyverse)

プログラム

RPythonSQL
1. 列名を指定して選択AwesomeAwesomeAwesome
2. 列番号を指定して選択AwesomeAwesome-
3. 列名を変更して選択AwesomeNot AwesomeAwesome
4. 全ての列を選択AwesomeAwesomeAwesome
5. 指定した列以外を選択AwesomeAwesomeAwesome
6. データ型を指定して選択AwesomeAwesome-
7. 欠損値が含まれる列を選択AwesomeNot Awesome-
8. パターンに一致する列を選択AwesomeAwesome-
9. 最後の列を選択AwesomeAwesome-

1. 列名を指定して選択

構文
df %>%
select(col1, col2)
サンプルコード
penguins %>%
select(species, island)

# # A tibble: 344 × 2
# species island
# <fct> <fct>
# 1 Adelie Torgersen
# 2 Adelie Torgersen
# 3 Adelie Torgersen
# 4 Adelie Torgersen
# 5 Adelie Torgersen
# 6 Adelie Torgersen
# 7 Adelie Torgersen
# 8 Adelie Torgersen
# 9 Adelie Torgersen
# 10 Adelie Torgersen
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

2. 列番号を指定して選択

構文
df %>%
select(1, 2)
サンプルコード
penguins %>%
select(1, 2)

# # A tibble: 344 × 2
# species island
# <fct> <fct>
# 1 Adelie Torgersen
# 2 Adelie Torgersen
# 3 Adelie Torgersen
# 4 Adelie Torgersen
# 5 Adelie Torgersen
# 6 Adelie Torgersen
# 7 Adelie Torgersen
# 8 Adelie Torgersen
# 9 Adelie Torgersen
# 10 Adelie Torgersen
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

3. 列名を変更して選択

構文
df %>%
select(new_col1 = col1, new_col2 = col2)
サンプルコード
penguins %>%
select(SPECIES = species, ISLAND = island)

# # A tibble: 344 × 2
# SPECIES ISLAND
# <fct> <fct>
# 1 Adelie Torgersen
# 2 Adelie Torgersen
# 3 Adelie Torgersen
# 4 Adelie Torgersen
# 5 Adelie Torgersen
# 6 Adelie Torgersen
# 7 Adelie Torgersen
# 8 Adelie Torgersen
# 9 Adelie Torgersen
# 10 Adelie Torgersen
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

4. 全ての列を選択

構文
df %>%
select(everything())
サンプルコード
penguins %>%
select(everything())

# # A tibble: 344 × 7
# species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
# <fct> <fct> <dbl> <dbl> <int> <int> <fct>
# 1 Adelie Torgersen 39.1 18.7 181 3750 male
# 2 Adelie Torgersen 39.5 17.4 186 3800 female
# 3 Adelie Torgersen 40.3 18 195 3250 female
# 4 Adelie Torgersen NA NA NA NA NA
# 5 Adelie Torgersen 36.7 19.3 193 3450 female
# 6 Adelie Torgersen 39.3 20.6 190 3650 male
# 7 Adelie Torgersen 38.9 17.8 181 3625 female
# 8 Adelie Torgersen 39.2 19.6 195 4675 male
# 9 Adelie Torgersen 34.1 18.1 193 3475 NA
# 10 Adelie Torgersen 42 20.2 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

5. 指定した列以外を選択

構文
df %>%
select(!col1, !col2)
サンプルコード
penguins %>%
select(!species, !island)

# # A tibble: 344 × 5
# bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
# <dbl> <dbl> <int> <int> <fct>
# 1 39.1 18.7 181 3750 male
# 2 39.5 17.4 186 3800 female
# 3 40.3 18 195 3250 female
# 4 NA NA NA NA NA
# 5 36.7 19.3 193 3450 female
# 6 39.3 20.6 190 3650 male
# 7 38.9 17.8 181 3625 female
# 8 39.2 19.6 195 4675 male
# 9 34.1 18.1 193 3475 NA
# 10 42 20.2 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

6. データ型を指定して選択

構文
df %>%
select(where(is.dtype))
サンプルコード
penguins %>%
select(where(is.numeric))

# # A tibble: 344 × 4
# bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
# <dbl> <dbl> <int> <int>
# 1 39.1 18.7 181 3750
# 2 39.5 17.4 186 3800
# 3 40.3 18 195 3250
# 4 NA NA NA NA
# 5 36.7 19.3 193 3450
# 6 39.3 20.6 190 3650
# 7 38.9 17.8 181 3625
# 8 39.2 19.6 195 4675
# 9 34.1 18.1 193 3475
# 10 42 20.2 190 4250
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

7. 欠損値が含まれる列を選択

構文
df %>%
select(where(~ any(is.na(.x))))
サンプルコード
penguins %>%
select(where(~ any(is.na(.x))))

# # A tibble: 344 × 5
# bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
# <dbl> <dbl> <int> <int> <fct>
# 1 39.1 18.7 181 3750 male
# 2 39.5 17.4 186 3800 female
# 3 40.3 18 195 3250 female
# 4 NA NA NA NA NA
# 5 36.7 19.3 193 3450 female
# 6 39.3 20.6 190 3650 male
# 7 38.9 17.8 181 3625 female
# 8 39.2 19.6 195 4675 male
# 9 34.1 18.1 193 3475 NA
# 10 42 20.2 190 4250 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

8. パターンに一致する列を選択

構文
df %>%
select(matches(r"(pattern)"))
サンプルコード
penguins %>%
select(matches(r"(_mm$)"))

# # A tibble: 344 × 3
# bill_length_mm bill_depth_mm flipper_length_mm
# <dbl> <dbl> <int>
# 1 39.1 18.7 181
# 2 39.5 17.4 186
# 3 40.3 18 195
# 4 NA NA NA
# 5 36.7 19.3 193
# 6 39.3 20.6 190
# 7 38.9 17.8 181
# 8 39.2 19.6 195
# 9 34.1 18.1 193
# 10 42 20.2 190
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

9. 最後の列を選択

構文
df %>%
select(last_col())
サンプルコード
penguins %>%
select(last_col())

# # A tibble: 344 × 1
# sex
# <fct>
# 1 male
# 2 female
# 3 female
# 4 NA
# 5 female
# 6 male
# 7 female
# 8 male
# 9 NA
# 10 NA
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows