Learn R Programming

mStats (version 3.2.2)

keep: Keep, drop and filter variables; arrange observations

Description

keep() or drop() variables within dataframe

arrange() sorts variables or columns

filter() keeps observations that satisfy specified conditions

Usage

keep(data, ...)

drop(data, ...)

arrange(data, ...)

filter(data, ...)

Arguments

data

Dataset

...

Variables or conditions in filter()

Value

Modified Dataset

Select Variables

keep() removes unspecified variables from the dataset. Variables are also rearranged based on the order they are mentioned.

keep(data, var1, var2, var3, etc)

drop() works the opposite of keep() removed specified variables from the dataset.

drop(data, var5, var6, var7, etc)

Sort

arrange() sorts the dataset by specifying variables. The minus symbol - indicates descending order.

arrange(data, var1, var2, -var3, var4)

Filter

filter keeps observations that meet the specified conditions. If conditions are specified by comma, they are joined by AND operators

filter(data, var1 == var2 | var3 > var4)

filter(data, var1 == var2, var3 > var4)

Examples

Run this code
# NOT RUN {
## use infert data
data(infert)
codebook(infert)

## DEMONSTRATION: KEEP
## suppose we want to keep education, age, induced and case
infert.new <- keep(infert, education, age, induced, case)
codebook(infert.new)

## use colon separator :
infert.new <- keep(infert, education:case)
codebook(infert.new)

## change the order of variables
infert.new <- keep(infert, stratum, pooled.stratum, education:spontaneous)
codebook(infert.new)



# }

Run the code above in your browser using DataLab