Learn R Programming

Kmisc (version 0.4.0-1)

extract: Extract Elements from a Named Object

Description

Extracts elements from an Robject with the names attribute set in a 'lazy' way. The first argument is the object, while the second is a set of names parsed from .... We return the object, including only the elements with names matched from ....

Usage

extract(x, ...)

Arguments

x
An Robject with a names attribute.
...
an optional number of 'names' to match in dat.

Details

We can be 'lazy' with how we pass names. 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 entries in ... 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 display a warning.

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