Learn R Programming

maditr (version 0.8.4)

columns: Selects columns or rows from the data set

Description

  • columns: select columns from dataset. There are four ways of column selection:

  1. Simply by column names

  2. By variable ranges, e. g. vs:carb. Alternatively, you can use '%to%' instead of colon: 'vs %to% carb'.

  3. With regular expressions. Characters which start with '^' or end with '$' considered as Perl-style regular expression patterns. For example, '^Petal' returns all variables started with 'Petal'. 'Width$' returns all variables which end with 'Width'. Pattern '^.' matches all variables and pattern '^.*my_str' is equivalent to contains "my_str"'.

  4. By character variables with interpolated parts. Expression in the curly brackets inside characters will be evaluated in the parent frame with text_expand. For example, a{1:3} will be transformed to the names 'a1', 'a2', 'a3'. 'cols' is just a shortcut for 'columns'. See examples.

  • rows: select rows from dataset by logical conditions.

Usage

columns(data, ...)

cols(data, ...)

rows(data, ...)

Value

data.frame/data.table

Arguments

data

data.table/data.frame

...

unquoted or quoted column names, regex selectors or variable ranges for 'columns' and logical conditions for 'rows'.

Examples

Run this code

## columns
mtcars %>%
    columns(vs:carb, cyl)
mtcars %>%
    columns(-am, -cyl)

# regular expression pattern
columns(iris, "^Petal") %>% head() # variables which start from 'Petal'
columns(iris, "Width$") %>% head() # variables which end with 'Width'
# move Species variable to the front.
# pattern "^." matches all variables
columns(iris, Species, "^.") %>% head()
# pattern "^.*i" means "contains 'i'"
columns(iris, "^.*i") %>% head()
# numeric indexing - all variables except Species
columns(iris, 1:4) %>% head()

# variable expansion
dims = c("Width", "Length")
columns(iris, "Petal.{dims}") %>% head()

# rows

mtcars %>%
    rows(am==0) %>%
    head()

# select rows with compound condition
mtcars %>%
    rows(am==0 & mpg>mean(mpg))

Run the code above in your browser using DataLab