Select or drop columns from a data.table
select(.df, ...)
A data.frame or data.table
Columns to select or drop.
Use named arguments, e.g. new_name = old_name, to rename selected variables.
tidyselect
compatible.
df <- data.table(
x1 = 1:3,
x2 = 1:3,
y = c("a", "b", "c"),
z = c("a", "b", "c")
)
df %>%
select(x1, y)
#> # A tidytable: 3 × 2
#> x1 y
#> <int> <chr>
#> 1 1 a
#> 2 2 b
#> 3 3 c
df %>%
select(x1:y)
#> # A tidytable: 3 × 3
#> x1 x2 y
#> <int> <int> <chr>
#> 1 1 1 a
#> 2 2 2 b
#> 3 3 3 c
df %>%
select(-y, -z)
#> # A tidytable: 3 × 2
#> x1 x2
#> <int> <int>
#> 1 1 1
#> 2 2 2
#> 3 3 3
df %>%
select(starts_with("x"), z)
#> # A tidytable: 3 × 3
#> x1 x2 z
#> <int> <int> <chr>
#> 1 1 1 a
#> 2 2 2 b
#> 3 3 3 c
df %>%
select(where(is.character), x1)
#> # A tidytable: 3 × 3
#> y z x1
#> <chr> <chr> <int>
#> 1 a a 1
#> 2 b b 2
#> 3 c c 3
df %>%
select(new = x1, y)
#> # A tidytable: 3 × 2
#> new y
#> <int> <chr>
#> 1 1 a
#> 2 2 b
#> 3 3 c