# 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")
# 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")
# 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")
Run the code above in your browser using DataLab