Learn R Programming

Kmisc (version 0.2.0)

extract: Extract Variables from a List / Data Frame

Description

Extract variables from a list / data.frame. The first argument is the list / data.frame, while the second is passed to and parsed from .... names passed to ... are first searched for in the global environment; if not found, we fall back to a character representation of the name. We return the list / data.frame, including only those whose names were found in ....

Usage

extract(dat, ...)

Arguments

dat
list or data.frame object, or other similar object with a names attribute
...
an optional number of 'names' to match in dat

Details

We can be 'lazy' with how we name the variables. The names passed to ... are not evaluated directly; rather, their character representation is taken and used for extraction. Furthermore, for a given item submitted, all text before a $ is removed.

First, symbols are parsed as characters, and named of dat are checked to see if they match any of names(dat). If not, we try to find the variable in the local search path, and match that against the names. If none of these are successful, we throw an error.

See Also

without, extract.re

Examples

Run this code
dat <- data.frame( x = c(1, 2, 3), y = c("a", "b", "c"), z=c(4, 5, 6) )
## all of these return identical output
dat[ names(dat) %in% c("x","z") ]
extract( dat, x, z)
extract( dat, dat$x, dat$z )

## we can even have a variable that includes names
a <- "z"
extract( dat, dat$x, a)

Run the code above in your browser using DataLab