keep: Keep or drop elements by name/criteria in data.frame/matrix

Description Usage Arguments Value Examples

View source: R/keep.R

Description

keep selects variables/elements from data.frame by their names or by criteria (see criteria). except drops variables/elements from data.frame by their names or by criteria. Names at the top-level can be unquoted (non-standard evaluation). For standard evaluation of parameters you can surround them by round brackets. See examples. Methods for list will apply keep/except to each element of the list separately. .keep/.except are versions which works with default_dataset.

Usage

1
2
3
4
5
6
7

Arguments

data

data.frame/matrix/list

...

column names of type character/numeric or criteria/logical functions

Value

object of the same type as data

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
data(iris)
keep(iris, Sepal.Length, Sepal.Width)  
except(iris, Species)

keep(iris, Species, other()) # move 'Species' to the first position
keep(iris, to("Petal.Width")) # keep all columns up to 'Species'

except(iris, perl("^Petal")) # remove columns which names start with 'Petal'

except(iris, 5) # remove fifth column

data(mtcars)
keep(mtcars, from("mpg") & to("qsec")) # keep columns from 'mpg' to 'qsec'
keep(mtcars, mpg %to% qsec) # the same result

 # standard and non-standard evaluation
 many_vars = c("am", "vs", "cyl")
 ## Not run: 
 keep(mtcars, many_vars) # error - names not found: 'many_vars'  
  
## End(Not run)
 keep(mtcars, (many_vars)) # ok
 
# character expansion
dfs = data.frame(
     a = 10 %r% 5,
     b_1 = 11 %r% 5,
     b_2 = 12 %r% 5,
     b_3 = 12 %r% 5,
     b_4 = 14 %r% 5,
     b_5 = 15 %r% 5 
 )
 i = 1:5
 keep(dfs, b_1 %to% b_5) 
 keep(dfs, text_expand("b_{i}")) # the same result  

expss documentation built on Jan. 8, 2021, 5:38 p.m.