drop_columns: Drop selected variables

Description Usage Arguments Details Examples

View source: R/drop_columns.r

Description

Quickly drop variables by either column names or positions.

Usage

1

Arguments

data

input data

ind

a vector of either names or column positions of the variables to be dropped.

Details

This function updates data.table object directly. Otherwise, output data will be returned matching input object class.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Load packages
library(data.table)

# Generate data
dt <- data.table(sapply(setNames(letters, letters), function(x) {assign(x, rnorm(10))}))
dt2 <- copy(dt)

# Drop variables by name
names(dt)
drop_columns(dt, letters[2L:25L])
names(dt)

# Drop variables by column position
names(dt2)
drop_columns(dt2, seq(2, 25))
names(dt2)

# Return from non-data.table input
df <- data.frame(sapply(setNames(letters, letters), function(x) {assign(x, rnorm(10))}))
drop_columns(df, letters[2L:25L])

Example output

 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
[1] "a" "z"
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
[1] "a" "z"
             a           z
1  -0.52090227  1.42238107
2   0.79923219 -0.45400805
3  -0.05970183 -1.37410299
4  -0.24395780  0.01785801
5  -0.34614703  1.06586258
6   0.73651015  0.81971655
7  -2.03719385 -1.88303934
8   0.25218612 -0.10095787
9  -0.86494359 -0.60545124
10  1.09284541  0.40399022

DataExplorer documentation built on Dec. 16, 2020, 1:07 a.m.