Learn R Programming

dformula (version 1.0)

select: Select a subset

Description

Selects the row and the varaibles by specifing a condition using a formula.

Usage

select(from, formula = .~., as = NULL, na.remove = FALSE, na.return = FALSE,...)

Value

Returns a data.frame object containing the selected elements.

Arguments

from

a data.frame object with variables

formula

a formula indicating the operation to create new varibles. Look at the detail section for explanantion

as

a character vector with names of new variables.

na.remove

a logical value indicating whether NA values should be removed

na.return

a logical value indicating whether only the observation with NA values should be shown

...

further arguments

Author

Alessio Serafini

Details

The formula is composed of two part:

column_names ~ row_conditions

the left-hand side are the names of the column to select, and the right-hand the operations to select the rows, using the I() function.

For example:

column_names1 + column_names2 ~ I(column_names1 == "a") + I(column_names2 > 4)

first the rows are selected if the observation in the column_names1 are equal to a and if the observation in the column_names2 are biggers than 4, then the column_names1 and column_names2 are returned.

If na.remove is set to TRUE, after the subsetting the observations with missing are removed.

Examples

Run this code
data("airquality")
dt <- airquality

## Selects columns and filter rows

select(from = dt, formula = .~ I(Ozone > 10 & Wind > 10))
select(from = dt, formula = Ozone ~ I(Wind > 10))
select(from = dt, formula = Ozone + Wind~ I(Ozone > 10))

## All rows and filter columns

select(from = dt, formula = Ozone ~ .)
select(from = dt, formula = Ozone + Wind ~ NULL)




Run the code above in your browser using DataLab