Choose or rename variables from a data.table. select() keeps only the variables you mention; rename() keeps all variables.

select(.data, ...)

select_vars(.data, ..., rm.dup = TRUE)

select_dt(.data, ..., cols = NULL, negate = FALSE)

select_mix(.data, ..., rm.dup = TRUE)

rename(.data, ...)

Arguments

.data

A data.table

...

One or more unquoted expressions separated by commas. Very flexible, same as tidyfst::select_dt and tidyfst::select_mix. details find select_dt.

rm.dup

Should duplicated columns be removed? Defaults to TRUE.

cols

(Optional)A numeric or character vector.

negate

Applicable when regular expression and "cols" is used. If TRUE, return the non-matched pattern. Default uses FALSE.

Value

A data.table

Details

No copy is made. Once you select or rename a data.table, they would be changed forever. select_vars could select across different data types, names and index. See examples.

select_dt and select_mix is the safe mode of select and select_vars, they keey the original copy but are not memory-efficient when dealing with large data sets.

See also

Examples

a = as.data.table(iris) a %>% select(1:3)
#> Sepal.Length Sepal.Width Petal.Length #> <num> <num> <num> #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 #> --- #> 146: 6.7 3.0 5.2 #> 147: 6.3 2.5 5.0 #> 148: 6.5 3.0 5.2 #> 149: 6.2 3.4 5.4 #> 150: 5.9 3.0 5.1
a
#> Sepal.Length Sepal.Width Petal.Length #> <num> <num> <num> #> 1: 5.1 3.5 1.4 #> 2: 4.9 3.0 1.4 #> 3: 4.7 3.2 1.3 #> 4: 4.6 3.1 1.5 #> 5: 5.0 3.6 1.4 #> --- #> 146: 6.7 3.0 5.2 #> 147: 6.3 2.5 5.0 #> 148: 6.5 3.0 5.2 #> 149: 6.2 3.4 5.4 #> 150: 5.9 3.0 5.1
a = as.data.table(iris) a %>% select_vars(is.factor,"Se")
#> Sepal.Length Sepal.Width Species #> <num> <num> <fctr> #> 1: 5.1 3.5 setosa #> 2: 4.9 3.0 setosa #> 3: 4.7 3.2 setosa #> 4: 4.6 3.1 setosa #> 5: 5.0 3.6 setosa #> --- #> 146: 6.7 3.0 virginica #> 147: 6.3 2.5 virginica #> 148: 6.5 3.0 virginica #> 149: 6.2 3.4 virginica #> 150: 5.9 3.0 virginica
a
#> Sepal.Length Sepal.Width Species #> <num> <num> <fctr> #> 1: 5.1 3.5 setosa #> 2: 4.9 3.0 setosa #> 3: 4.7 3.2 setosa #> 4: 4.6 3.1 setosa #> 5: 5.0 3.6 setosa #> --- #> 146: 6.7 3.0 virginica #> 147: 6.3 2.5 virginica #> 148: 6.5 3.0 virginica #> 149: 6.2 3.4 virginica #> 150: 5.9 3.0 virginica
a = as.data.table(iris) a %>% select("Se") %>% rename(sl = Sepal.Length, sw = Sepal.Width)
#> sl sw #> <num> <num> #> 1: 5.1 3.5 #> 2: 4.9 3.0 #> 3: 4.7 3.2 #> 4: 4.6 3.1 #> 5: 5.0 3.6 #> --- #> 146: 6.7 3.0 #> 147: 6.3 2.5 #> 148: 6.5 3.0 #> 149: 6.2 3.4 #> 150: 5.9 3.0
a
#> sl sw #> <num> <num> #> 1: 5.1 3.5 #> 2: 4.9 3.0 #> 3: 4.7 3.2 #> 4: 4.6 3.1 #> 5: 5.0 3.6 #> --- #> 146: 6.7 3.0 #> 147: 6.3 2.5 #> 148: 6.5 3.0 #> 149: 6.2 3.4 #> 150: 5.9 3.0
DT = data.table(a=1:2,b=3:4,c=5:6) DT
#> a b c #> <int> <int> <int> #> 1: 1 3 5 #> 2: 2 4 6
DT %>% rename(B=b)
#> a B c #> <int> <int> <int> #> 1: 1 3 5 #> 2: 2 4 6