Select specific column(s) via various ways. One can select columns by their column names, indexes or regular expression recognizing the column name(s).

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

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

Arguments

.data

data.frame

...

List of variables or name-value pairs of summary/modifications functions. It can also recieve conditional function to select columns. When starts with `-`(minus symbol) or `!`, return the negative columns.

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.

rm.dup

Should duplicated columns be removed? Defaults to TRUE.

Value

data.table

See also

Examples

iris %>% select_dt(Species)
#>        Species
#>         <fctr>
#>   1:    setosa
#>   2:    setosa
#>   3:    setosa
#>   4:    setosa
#>   5:    setosa
#>  ---          
#> 146: virginica
#> 147: virginica
#> 148: virginica
#> 149: virginica
#> 150: virginica
iris %>% select_dt(Sepal.Length,Sepal.Width)
#>      Sepal.Length Sepal.Width
#>             <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
iris %>% select_dt(Sepal.Length:Petal.Length)
#>      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
iris %>% select_dt(-Sepal.Length)
#>      Sepal.Width Petal.Length Petal.Width   Species
#>            <num>        <num>       <num>    <fctr>
#>   1:         3.5          1.4         0.2    setosa
#>   2:         3.0          1.4         0.2    setosa
#>   3:         3.2          1.3         0.2    setosa
#>   4:         3.1          1.5         0.2    setosa
#>   5:         3.6          1.4         0.2    setosa
#>  ---                                               
#> 146:         3.0          5.2         2.3 virginica
#> 147:         2.5          5.0         1.9 virginica
#> 148:         3.0          5.2         2.0 virginica
#> 149:         3.4          5.4         2.3 virginica
#> 150:         3.0          5.1         1.8 virginica
iris %>% select_dt(-Sepal.Length,-Petal.Length)
#>      Sepal.Width Petal.Width   Species
#>            <num>       <num>    <fctr>
#>   1:         3.5         0.2    setosa
#>   2:         3.0         0.2    setosa
#>   3:         3.2         0.2    setosa
#>   4:         3.1         0.2    setosa
#>   5:         3.6         0.2    setosa
#>  ---                                  
#> 146:         3.0         2.3 virginica
#> 147:         2.5         1.9 virginica
#> 148:         3.0         2.0 virginica
#> 149:         3.4         2.3 virginica
#> 150:         3.0         1.8 virginica
iris %>% select_dt(-(Sepal.Length:Petal.Length))
#>      Petal.Width   Species
#>            <num>    <fctr>
#>   1:         0.2    setosa
#>   2:         0.2    setosa
#>   3:         0.2    setosa
#>   4:         0.2    setosa
#>   5:         0.2    setosa
#>  ---                      
#> 146:         2.3 virginica
#> 147:         1.9 virginica
#> 148:         2.0 virginica
#> 149:         2.3 virginica
#> 150:         1.8 virginica
iris %>% select_dt(c("Sepal.Length","Sepal.Width"))
#>      Sepal.Length Sepal.Width
#>             <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
iris %>% select_dt(-c("Sepal.Length","Sepal.Width"))
#>      Petal.Length Petal.Width   Species
#>             <num>       <num>    <fctr>
#>   1:          1.4         0.2    setosa
#>   2:          1.4         0.2    setosa
#>   3:          1.3         0.2    setosa
#>   4:          1.5         0.2    setosa
#>   5:          1.4         0.2    setosa
#>  ---                                   
#> 146:          5.2         2.3 virginica
#> 147:          5.0         1.9 virginica
#> 148:          5.2         2.0 virginica
#> 149:          5.4         2.3 virginica
#> 150:          5.1         1.8 virginica
iris %>% select_dt(1)
#>      Sepal.Length
#>             <num>
#>   1:          5.1
#>   2:          4.9
#>   3:          4.7
#>   4:          4.6
#>   5:          5.0
#>  ---             
#> 146:          6.7
#> 147:          6.3
#> 148:          6.5
#> 149:          6.2
#> 150:          5.9
iris %>% select_dt(-1)
#>      Sepal.Width Petal.Length Petal.Width   Species
#>            <num>        <num>       <num>    <fctr>
#>   1:         3.5          1.4         0.2    setosa
#>   2:         3.0          1.4         0.2    setosa
#>   3:         3.2          1.3         0.2    setosa
#>   4:         3.1          1.5         0.2    setosa
#>   5:         3.6          1.4         0.2    setosa
#>  ---                                               
#> 146:         3.0          5.2         2.3 virginica
#> 147:         2.5          5.0         1.9 virginica
#> 148:         3.0          5.2         2.0 virginica
#> 149:         3.4          5.4         2.3 virginica
#> 150:         3.0          5.1         1.8 virginica
iris %>% select_dt(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
iris %>% select_dt(-(1:3))
#>      Petal.Width   Species
#>            <num>    <fctr>
#>   1:         0.2    setosa
#>   2:         0.2    setosa
#>   3:         0.2    setosa
#>   4:         0.2    setosa
#>   5:         0.2    setosa
#>  ---                      
#> 146:         2.3 virginica
#> 147:         1.9 virginica
#> 148:         2.0 virginica
#> 149:         2.3 virginica
#> 150:         1.8 virginica
iris %>% select_dt(1,3)
#>      Sepal.Length Petal.Length
#>             <num>        <num>
#>   1:          5.1          1.4
#>   2:          4.9          1.4
#>   3:          4.7          1.3
#>   4:          4.6          1.5
#>   5:          5.0          1.4
#>  ---                          
#> 146:          6.7          5.2
#> 147:          6.3          5.0
#> 148:          6.5          5.2
#> 149:          6.2          5.4
#> 150:          5.9          5.1
iris %>% select_dt("Pe")
#>      Petal.Length Petal.Width
#>             <num>       <num>
#>   1:          1.4         0.2
#>   2:          1.4         0.2
#>   3:          1.3         0.2
#>   4:          1.5         0.2
#>   5:          1.4         0.2
#>  ---                         
#> 146:          5.2         2.3
#> 147:          5.0         1.9
#> 148:          5.2         2.0
#> 149:          5.4         2.3
#> 150:          5.1         1.8
iris %>% select_dt(-"Se")
#>      Petal.Length Petal.Width   Species
#>             <num>       <num>    <fctr>
#>   1:          1.4         0.2    setosa
#>   2:          1.4         0.2    setosa
#>   3:          1.3         0.2    setosa
#>   4:          1.5         0.2    setosa
#>   5:          1.4         0.2    setosa
#>  ---                                   
#> 146:          5.2         2.3 virginica
#> 147:          5.0         1.9 virginica
#> 148:          5.2         2.0 virginica
#> 149:          5.4         2.3 virginica
#> 150:          5.1         1.8 virginica
iris %>% select_dt(!"Se")
#>      Petal.Length Petal.Width   Species
#>             <num>       <num>    <fctr>
#>   1:          1.4         0.2    setosa
#>   2:          1.4         0.2    setosa
#>   3:          1.3         0.2    setosa
#>   4:          1.5         0.2    setosa
#>   5:          1.4         0.2    setosa
#>  ---                                   
#> 146:          5.2         2.3 virginica
#> 147:          5.0         1.9 virginica
#> 148:          5.2         2.0 virginica
#> 149:          5.4         2.3 virginica
#> 150:          5.1         1.8 virginica
iris %>% select_dt("Pe",negate = TRUE)
#>      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
iris %>% select_dt("Pe|Sp")
#>      Petal.Length Petal.Width   Species
#>             <num>       <num>    <fctr>
#>   1:          1.4         0.2    setosa
#>   2:          1.4         0.2    setosa
#>   3:          1.3         0.2    setosa
#>   4:          1.5         0.2    setosa
#>   5:          1.4         0.2    setosa
#>  ---                                   
#> 146:          5.2         2.3 virginica
#> 147:          5.0         1.9 virginica
#> 148:          5.2         2.0 virginica
#> 149:          5.4         2.3 virginica
#> 150:          5.1         1.8 virginica
iris %>% select_dt(cols = 2:3)
#>      Sepal.Width Petal.Length
#>            <num>        <num>
#>   1:         3.5          1.4
#>   2:         3.0          1.4
#>   3:         3.2          1.3
#>   4:         3.1          1.5
#>   5:         3.6          1.4
#>  ---                         
#> 146:         3.0          5.2
#> 147:         2.5          5.0
#> 148:         3.0          5.2
#> 149:         3.4          5.4
#> 150:         3.0          5.1
iris %>% select_dt(cols = 2:3,negate = TRUE)
#>      Sepal.Length Petal.Width   Species
#>             <num>       <num>    <fctr>
#>   1:          5.1         0.2    setosa
#>   2:          4.9         0.2    setosa
#>   3:          4.7         0.2    setosa
#>   4:          4.6         0.2    setosa
#>   5:          5.0         0.2    setosa
#>  ---                                   
#> 146:          6.7         2.3 virginica
#> 147:          6.3         1.9 virginica
#> 148:          6.5         2.0 virginica
#> 149:          6.2         2.3 virginica
#> 150:          5.9         1.8 virginica
iris %>% select_dt(cols = c("Sepal.Length","Sepal.Width"))
#>      Sepal.Length Sepal.Width
#>             <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
iris %>% select_dt(cols = names(iris)[2:3])
#>      Sepal.Width Petal.Length
#>            <num>        <num>
#>   1:         3.5          1.4
#>   2:         3.0          1.4
#>   3:         3.2          1.3
#>   4:         3.1          1.5
#>   5:         3.6          1.4
#>  ---                         
#> 146:         3.0          5.2
#> 147:         2.5          5.0
#> 148:         3.0          5.2
#> 149:         3.4          5.4
#> 150:         3.0          5.1

iris %>% select_dt(is.factor)
#>        Species
#>         <fctr>
#>   1:    setosa
#>   2:    setosa
#>   3:    setosa
#>   4:    setosa
#>   5:    setosa
#>  ---          
#> 146: virginica
#> 147: virginica
#> 148: virginica
#> 149: virginica
#> 150: virginica
iris %>% select_dt(-is.factor)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#>             <num>       <num>        <num>       <num>
#>   1:          5.1         3.5          1.4         0.2
#>   2:          4.9         3.0          1.4         0.2
#>   3:          4.7         3.2          1.3         0.2
#>   4:          4.6         3.1          1.5         0.2
#>   5:          5.0         3.6          1.4         0.2
#>  ---                                                  
#> 146:          6.7         3.0          5.2         2.3
#> 147:          6.3         2.5          5.0         1.9
#> 148:          6.5         3.0          5.2         2.0
#> 149:          6.2         3.4          5.4         2.3
#> 150:          5.9         3.0          5.1         1.8
iris %>% select_dt(!is.factor)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#>             <num>       <num>        <num>       <num>
#>   1:          5.1         3.5          1.4         0.2
#>   2:          4.9         3.0          1.4         0.2
#>   3:          4.7         3.2          1.3         0.2
#>   4:          4.6         3.1          1.5         0.2
#>   5:          5.0         3.6          1.4         0.2
#>  ---                                                  
#> 146:          6.7         3.0          5.2         2.3
#> 147:          6.3         2.5          5.0         1.9
#> 148:          6.5         3.0          5.2         2.0
#> 149:          6.2         3.4          5.4         2.3
#> 150:          5.9         3.0          5.1         1.8

# select_mix could provide flexible mix selection
select_mix(iris, Species,"Sepal.Length")
#>        Species Sepal.Length
#>         <fctr>        <num>
#>   1:    setosa          5.1
#>   2:    setosa          4.9
#>   3:    setosa          4.7
#>   4:    setosa          4.6
#>   5:    setosa          5.0
#>  ---                       
#> 146: virginica          6.7
#> 147: virginica          6.3
#> 148: virginica          6.5
#> 149: virginica          6.2
#> 150: virginica          5.9
select_mix(iris,1:2,is.factor)
#>      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

select_mix(iris,Sepal.Length,is.numeric)
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width
#>             <num>       <num>        <num>       <num>
#>   1:          5.1         3.5          1.4         0.2
#>   2:          4.9         3.0          1.4         0.2
#>   3:          4.7         3.2          1.3         0.2
#>   4:          4.6         3.1          1.5         0.2
#>   5:          5.0         3.6          1.4         0.2
#>  ---                                                  
#> 146:          6.7         3.0          5.2         2.3
#> 147:          6.3         2.5          5.0         1.9
#> 148:          6.5         3.0          5.2         2.0
#> 149:          6.2         3.4          5.4         2.3
#> 150:          5.9         3.0          5.1         1.8
# set rm.dup to FALSE could save the duplicated column names
select_mix(iris,Sepal.Length,is.numeric,rm.dup = FALSE)
#>      Sepal.Length Sepal.Length Sepal.Width Petal.Length Petal.Width
#>             <num>        <num>       <num>        <num>       <num>
#>   1:          5.1          5.1         3.5          1.4         0.2
#>   2:          4.9          4.9         3.0          1.4         0.2
#>   3:          4.7          4.7         3.2          1.3         0.2
#>   4:          4.6          4.6         3.1          1.5         0.2
#>   5:          5.0          5.0         3.6          1.4         0.2
#>  ---                                                               
#> 146:          6.7          6.7         3.0          5.2         2.3
#> 147:          6.3          6.3         2.5          5.0         1.9
#> 148:          6.5          6.5         3.0          5.2         2.0
#> 149:          6.2          6.2         3.4          5.4         2.3
#> 150:          5.9          5.9         3.0          5.1         1.8