data_extract | R Documentation |
data_extract()
(or its alias extract()
) is similar to $
. It extracts
either a single column or element from an object (e.g., a data frame, list),
or multiple columns resp. elements.
data_extract(data, select, ...)
## S3 method for class 'data.frame'
data_extract(
data,
select,
name = NULL,
extract = "all",
as_data_frame = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...
)
data |
The object to subset. Methods are currently available for data frames and data frame extensions (e.g., tibbles). |
select |
Variables that will be included when performing the required tasks. Can be either
If |
... |
For use by future methods. |
name |
An optional argument that specifies the column to be used as
names for the vector elements after extraction. Must be specified either
as literal variable name (e.g., |
extract |
String, indicating which element will be extracted when |
as_data_frame |
Logical, if |
ignore_case |
Logical, if |
regex |
Logical, if |
verbose |
Toggle warnings. |
data_extract()
can be used to select multiple variables or pull a
single variable from a data frame. Thus, the return value is by default not
type safe - data_extract()
either returns a vector or a data frame.
When select
is the name of a single column, or when select only matches
one column, a vector is returned. A single variable is also returned when
extract
is either "first
or "last"
. Setting as_data_frame
to TRUE
overrides this behaviour and always returns a data frame.
When select
is a character vector containing more than one column name (or
a numeric vector with more than one valid column indices), or when select
uses one of the supported select-helpers that match multiple columns, a
data frame is returned. Setting as_data_frame
to TRUE
always returns
a data frame.
A vector (or a data frame) containing the extracted element, or
NULL
if no matching variable was found.
# single variable
data_extract(mtcars, cyl, name = gear)
data_extract(mtcars, "cyl", name = gear)
data_extract(mtcars, -1, name = gear)
data_extract(mtcars, cyl, name = 0)
data_extract(mtcars, cyl, name = "row.names")
# selecting multiple variables
head(data_extract(iris, starts_with("Sepal")))
head(data_extract(iris, ends_with("Width")))
head(data_extract(iris, 2:4))
# select first of multiple variables
data_extract(iris, starts_with("Sepal"), extract = "first")
# select first of multiple variables, return as data frame
head(data_extract(iris, starts_with("Sepal"), extract = "first", as_data_frame = TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.