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, ...)
.data | A data.table |
---|---|
... | One or more unquoted expressions separated by commas.
Very flexible, same as |
rm.dup | Should duplicated columns be removed? Defaults to |
cols | (Optional)A numeric or character vector. |
negate | Applicable when regular expression and "cols" is used.
If |
A data.table
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.
#> 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.1a#> 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#> 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 virginicaa#> 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#> 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.0a#> 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 b c #> <int> <int> <int> #> 1: 1 3 5 #> 2: 2 4 6DT %>% rename(B=b)#> a B c #> <int> <int> <int> #> 1: 1 3 5 #> 2: 2 4 6