strapply {gsubfn} | R Documentation |
Similar to "'gsubfn'"
except instead of performing substitutions
it returns the output of "'FUN'"
.
strapply(X, pattern, FUN = function(x, ...) x, ..., simplify = FALSE, USE.NAMES = FALSE)
X |
list or (atomic) vector of character strings to be used. |
pattern |
character string containing a regular expression (or
character string for "'fixed = TRUE')" to be matched in the
given character vector.
|
FUN |
the function or formula to be applied to each element of
"'X'" . See discussion in gsubfn . |
... |
optional arguments to "'gsubfn'" . |
simplify |
logical or function. If logical, should the result be
simplified to a vector or matrix, as in "sapply" if possible?
If function, that function is applied to the result with each
component of the result passed as a separate argument. Typically
if the form is used it will typically be specified as rbind. |
USE.NAMES |
logical; if "'TRUE'" and if "'X'" is character, use
"'X'" as
'names' for the result unless it had names already.
|
For each character string in "X"
the pattern is repeatedly
matched,
each such match along with
backreferences, if any, are passed to
the function "FUN"
and the output is returned as a list.
A list of character strings.
strapply("12;34:56,89,,12", "[0-9]+") # separate leading digits from rest of string # creating a 3 column matrix: original string, digits, rest s <- c("123abc", "12cd34", "1e23") t(strapply(s, "^([[:digit:]]+)(.*)", c, simplify = TRUE)) # same but create data.frame strapply(s, "^([[:digit:]]+)(.*)", ~ data.frame(string, digits, rest), simplify = rbind) # running window of 5 characters using 0-lookahead perl regexp x <- "abcdefghijkl" strapply(x, "(.)(?=(....))", paste0, backref = -2, perl = TRUE)[[1]] # find second CPU_SPEED value given lines of config file Lines <- c("DEVICE = 'PC'", "CPU_SPEED = '1999', '233'") parms <- strapply(Lines, "[^ ',=]+", c, USE.NAMES = TRUE) parms <- lapply(parms, "[", -1) parms[["CPU_SPEED"]][2] ## Not run: # convert to chron library(chron) x <- c("01/15/2005 23:32:45", "02/27/2005 01:22:30") x.chron <- strapply(x, "(../../....) (..:..:..)", chron, backref = -2, simplify = c) ## End(Not run)