Learn R Programming

tidyft (version 0.9.20)

select: Select/rename variables by name

Description

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

Usage

select(.data, ...)

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

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

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

rename(.data, ...)

Value

A data.table

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.

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

select_dt, rename_dt

Examples

Run this code

  a = as.data.table(iris)
  a %>% select(1:3)
  a

  a = as.data.table(iris)
  a %>% select_vars(is.factor,"Se")
  a

  a = as.data.table(iris)
  a %>% select("Se") %>%
    rename(sl = Sepal.Length,
    sw = Sepal.Width)
  a


DT = data.table(a=1:2,b=3:4,c=5:6)
DT
DT %>% rename(B=b)

Run the code above in your browser using DataLab