Simple tool to perform multiple operations to character vectors.

string_ops_alias(op = NULL, pre_unik = NULL, namespace = NULL)

string_ops(
  x,
  ...,
  op = NULL,
  pre_unik = NULL,
  namespace = NULL,
  envir = parent.frame()
)

st_ops(
  x,
  ...,
  op = NULL,
  pre_unik = NULL,
  namespace = NULL,
  envir = parent.frame()
)

Arguments

op

Character vector or NULL (default). Character scalar containing the comma separated values of operations to perform to the vector. The 50+ operations are detailed in the help page of string_magic(). Note that if this argument is provided, then the values in ... are ignored.

pre_unik

Logical scalar, default is NULL. Whether to first unique the vector before applying the possibly costly string operations, and merging back the result. For very large vectors with repeated values the time gained can be substantial. By default, this is TRUE for vector of length 1M or more.

namespace

Character scalar or NULL (default). Only useful for package developers. As a regular end-user you shouldn't care! If your package uses string_magic, you should care. It is useful only if your package uses 'custom' string_magic operations, set with string_magic_register_fun() or string_magic_register_ops().

If so pass the name of your package in this argument so that your function can access the new string_magic operations defined within your package.

x

A character vector. If not a character vector but atomistic (i.e. not a list), it will be converted to a character vector.

...

Character scalars. Character scalar containing the comma separated values of operations to perform to the vector. The 50+ operations are detailed in the help page of string_magic().

envir

Environment in which to evaluate the interpolations if the flag "magic" is provided. Default is parent.frame().

Value

In general it returns a character vector. It may be of a length different from the original one, depending on the operations performed.

Details

This function is a simple wrapper around string_magic. Formally, string_ops(x, "op1, op2") is equivalent to string_magic("{op1, op2 ? x}").

Functions

  • string_ops_alias(): string_ops alias with custom defaults

  • st_ops(): Alias to string_ops

See also

Author

Laurent R. Berge

Examples


# data on car models
cars = row.names(mtcars)

# let's get the brands starting with an "m"
string_ops(cars, "'i/^m'get, x, unik")
#> [1] "Mazda"    "Merc"     "Maserati"

# Explainer:
# 'i/^m'get: keeps only the elements starting with an m,
#            i/ is the 'regex-flag' "ignore" to ignore the case
#            ^m means "starts with an m" in regex language
# x: extracts the first pattern. The default pattern is "[[:alnum:]]+"
#    which means an alpha-numeric word
# unik: applies unique() to the vector
# => see help in ?string_magic for more details on the operations


# let's get the 3 largest numbers appearing in the car models
string_ops(cars, "'\\d+'x, rm, unik, num, dsort, 3 first")
#> [1] 914 710 450

# Explainer:
# '\d+'x: extracts the first pattern, the pattern meaning "a succession"
#          of digits in regex language
# rm: removes elements equal to the empty string (default behavior)
# unik: applies unique() to the vector
# num: converts to numeric
# dsort: sorts in decreasing order
# 3 first: keeps only the first three elements

# You can use several character vectors as operations:
string_ops(cars, 
           "'\\d+'x, rm, unik",
           "num, dsort, 3 first")
#> [1] 914 710 450