pick {str2str} | R Documentation |
Extract Elements From a (Atomic) Vector
Description
pick
extracts the elements from a (atomic) vector that meet certain criteria:
1) using exact values or regular expressions (pat
), 2) inclusion vs.
exclusion of the value/expression (not
), 3) based on elements or names (nm
).
Primarily for character vectors, but can be used with other typeof.
Usage
pick(x, val, pat = FALSE, not = FALSE, nm = FALSE, fixed = FALSE)
Arguments
x |
atomic vector or an object with names (e.g., data.frame) if |
val |
atomic vector specifying which elements of |
pat |
logical vector of length 1 specifying whether |
not |
logical vector of length 1 specifying whether |
nm |
logical vector of length 1 specifying whether |
fixed |
logical vector of length 1 specifying whether |
Details
pick
allows for 8 different ways to extract elements from a (atomic) vector
created by the 2x2x2 combination of logical arguments pat
, not
, and nm
.
When pat
= FALSE (default), pick
uses is.element
(essentially
match
) and requires exact matching of val
in x
. When pat
= TRUE, pick
uses grepl
and allows for partial matching of val
in x
and/or regular expressions if fixed
= FALSE (default).
When dealing with regular expressions via pat
= TRUE and fixed
= FALSE,
certain symbols within val
are not interpreted as literal characters and
instead have special meanings. Some of the most commonly used symbols are .
= any character, "|"
= logical or, "^"
= starts with, "\n"
= new line,
"\t"
= tab.
Value
a subset of x
that only includes the elements which meet the criteria
specified by the function call.
Examples
# pedagogical cases
chr <- setNames(object = c("one","two","three","four","five"), nm = as.character(1:5))
# 1) pat = FALSE, not = FALSE, nm = FALSE
pick(x = chr, val = c("one","five"), pat = FALSE, not = FALSE, nm = FALSE)
# 2) pat = FALSE, not = FALSE, nm = TRUE
pick(x = chr, val = c("1","5"), pat = FALSE, not = FALSE, nm = TRUE)
# 3) pat = FALSE, not = TRUE, nm = FALSE
pick(x = chr, val = c("two","three","four"), pat = FALSE, not = TRUE, nm = FALSE)
# 4) pat = FALSE, not = TRUE, nm = TRUE
pick(x = chr, val = c("2","3","4"), pat = FALSE, not = TRUE, nm = TRUE)
# 5) pat = TRUE, not = FALSE, nm = FALSE
pick(x = chr, val = "n|v", pat = TRUE, not = FALSE, nm = FALSE)
# 6) pat = TRUE, not = FALSE, nm = TRUE
pick(x = chr, val = "1|5", pat = TRUE, not = FALSE, nm = TRUE)
# 7) pat = TRUE, not = TRUE, nm = FALSE
pick(x = chr, val = "t|r", pat = TRUE, not = TRUE, nm = FALSE)
# 8) pat = TRUE, not = TRUE, nm = TRUE
pick(x = chr, val = c("2|3|4"), pat = TRUE, not = TRUE, nm = TRUE)
datasets <- data()[["results"]][, "Item"]
# actual use cases
pick(x = datasets, val = c("attitude","mtcars","airquality"),
not = TRUE) # all but the three most common datasets used in `str2str` package examples
pick(x = datasets, val = "state", pat = TRUE) # only datasets that contain "state"
pick(x = datasets, val = "state.*state", pat = TRUE) # only datasets that have
# "state" twice in their name
pick(x = datasets, val = "US|UK", pat = TRUE) # only datasets that contain
# "US" or "UK"
pick(x = datasets, val = "^US|^UK", pat = TRUE) # only datasets that start with
# "US" or "UK"
pick(x = datasets, val = "k.*o|o.*k", pat = TRUE) # only datasets containing both
# "k" and "o"