Spot patterns in strings.

string_spot(s, pattern, value = TRUE, ...)
string_spoti(s, pattern, ...)
string_spotl(s, pattern, ...)
string_spotm(s, pattern, invert = FALSE, ...)
string_replace(s, search, replace, ...)
string_remove(s, remove, ...)
string_countm(s, pattern, ...)
string_locate(s, pattern, invert = FALSE, ...)

Arguments

s

A string (character) vector.

pattern

A regular expression pattern.

value

Boolean value (TRUE/FALSE) for whether to return the actual values (the former) or the indices (the latter).

...

Parameters to pass to grep(), grepl(), gsub(), or gregexpr().

invert

Boolean value (TRUE/FALSE) for whether to find actual matches (FALSE; default) or non-matches (TRUE).

search

A pattern to search.

replace

The string to replace the searched pattern.

remove

The string to remove.

Value

Character vector for all functions except the string_spotl(), string_spoti(), and string_countm() functions, which produce Boolean, numeric, and numeric vectors respectively.

Details

The function string_spot() subsets a vector to the values matching a given pattern. Optional inputs get passed to grep(). Synonyms are s_spot() and spot().

The function string_spoti() subsets a vector to the indices matching a given pattern. Optional inputs get passed to grep(). Synonyms are s_spoti() and spoti().

The function string_spotl() detects whether a pattern exists in a vector, outputting a Boolean value (TRUE/FALSE). Optional inputs get passed to grepl(). Synonyms are s_spotl() and spotl().

The string_spotm() function spots pattern matches and returns NA if none are found. Optional inputs get passed to grepl(). Synonyms are s_spotm() and spotm().

The string_replace() function acts the same as gsub() with the inputs ordered differently. Optional inputs get passed to gsub(). Synonyms are s_replace(), search_replace(), sr(), find_replace(), and fr().

The string_remove() function blanks out a matching pattern. Optional inputs get passed to gsub(). Synonym is s_remove().

The string_countm() function counts the number of matches in a string. Optional inputs get passed to gregexpr(). Synonyms are s_countm() and countm().

The string_locate() functions produces the character positions at where the pattern match are found--non-matches are produced if invert = TRUE. Optional inputs are passed to gregexpr(). Synonyms are s_locate() and locate().

See also

Examples

rn <- rownames(mtcars) string_spot(rn, "^M")
#> [1] "Mazda RX4" "Mazda RX4 Wag" "Merc 240D" "Merc 230" #> [5] "Merc 280" "Merc 280C" "Merc 450SE" "Merc 450SL" #> [9] "Merc 450SLC" "Maserati Bora"
string_spot(rn, '^M', invert = TRUE)
#> [1] "Datsun 710" "Hornet 4 Drive" "Hornet Sportabout" #> [4] "Valiant" "Duster 360" "Cadillac Fleetwood" #> [7] "Lincoln Continental" "Chrysler Imperial" "Fiat 128" #> [10] "Honda Civic" "Toyota Corolla" "Toyota Corona" #> [13] "Dodge Challenger" "AMC Javelin" "Camaro Z28" #> [16] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2" #> [19] "Lotus Europa" "Ford Pantera L" "Ferrari Dino" #> [22] "Volvo 142E"
string_spoti(rn, "^M")
#> [1] 1 2 8 9 10 11 12 13 14 31
string_spotl(rn, "^M")
#> [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE #> [13] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE #> [25] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
string_spotm(rn, "^M")
#> [1] "Mazda RX4" "Mazda RX4 Wag" NA NA #> [5] NA NA NA "Merc 240D" #> [9] "Merc 230" "Merc 280" "Merc 280C" "Merc 450SE" #> [13] "Merc 450SL" "Merc 450SLC" NA NA #> [17] NA NA NA NA #> [21] NA NA NA NA #> [25] NA NA NA NA #> [29] NA NA "Maserati Bora" NA
string_replace(rn, "^M", "Z")
#> [1] "Zazda RX4" "Zazda RX4 Wag" "Datsun 710" #> [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant" #> [7] "Duster 360" "Zerc 240D" "Zerc 230" #> [10] "Zerc 280" "Zerc 280C" "Zerc 450SE" #> [13] "Zerc 450SL" "Zerc 450SLC" "Cadillac Fleetwood" #> [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128" #> [19] "Honda Civic" "Toyota Corolla" "Toyota Corona" #> [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28" #> [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2" #> [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino" #> [31] "Zaserati Bora" "Volvo 142E"
string_remove(rn, "^M")
#> [1] "azda RX4" "azda RX4 Wag" "Datsun 710" #> [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant" #> [7] "Duster 360" "erc 240D" "erc 230" #> [10] "erc 280" "erc 280C" "erc 450SE" #> [13] "erc 450SL" "erc 450SLC" "Cadillac Fleetwood" #> [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128" #> [19] "Honda Civic" "Toyota Corolla" "Toyota Corona" #> [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28" #> [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2" #> [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino" #> [31] "aserati Bora" "Volvo 142E"
string_countm(rn, 'a')
#> [1] 2 3 1 0 1 2 0 0 0 0 0 0 0 0 2 1 1 1 1 2 2 1 1 2 1 1 0 1 2 1 3 0
string_locate(rn, 'a')
#> [[1]] #> [1] 2 5 #> #> [[2]] #> [1] 2 5 12 #> #> [[3]] #> [1] 2 #> #> [[4]] #> [1] NA #> #> [[5]] #> [1] 13 #> #> [[6]] #> [1] 2 5 #> #> [[7]] #> [1] NA #> #> [[8]] #> [1] NA #> #> [[9]] #> [1] NA #> #> [[10]] #> [1] NA #> #> [[11]] #> [1] NA #> #> [[12]] #> [1] NA #> #> [[13]] #> [1] NA #> #> [[14]] #> [1] NA #> #> [[15]] #> [1] 2 7 #> #> [[16]] #> [1] 18 #> #> [[17]] #> [1] 16 #> #> [[18]] #> [1] 3 #> #> [[19]] #> [1] 5 #> #> [[20]] #> [1] 6 14 #> #> [[21]] #> [1] 6 13 #> #> [[22]] #> [1] 9 #> #> [[23]] #> [1] 6 #> #> [[24]] #> [1] 2 4 #> #> [[25]] #> [1] 6 #> #> [[26]] #> [1] 3 #> #> [[27]] #> [1] NA #> #> [[28]] #> [1] 12 #> #> [[29]] #> [1] 7 12 #> #> [[30]] #> [1] 5 #> #> [[31]] #> [1] 2 6 13 #> #> [[32]] #> [1] NA #>
string_locate(rn, 'a', invert = TRUE)
#> [[1]] #> [1] 1 3 4 6 7 8 9 #> #> [[2]] #> [1] 1 3 4 6 7 8 9 10 11 13 #> #> [[3]] #> [1] 1 3 4 5 6 7 8 9 10 #> #> [[4]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #> #> [[5]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 #> #> [[6]] #> [1] 1 3 4 6 7 #> #> [[7]] #> [1] 1 2 3 4 5 6 7 8 9 10 #> #> [[8]] #> [1] 1 2 3 4 5 6 7 8 9 #> #> [[9]] #> [1] 1 2 3 4 5 6 7 8 #> #> [[10]] #> [1] 1 2 3 4 5 6 7 8 #> #> [[11]] #> [1] 1 2 3 4 5 6 7 8 9 #> #> [[12]] #> [1] 1 2 3 4 5 6 7 8 9 10 #> #> [[13]] #> [1] 1 2 3 4 5 6 7 8 9 10 #> #> [[14]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 #> #> [[15]] #> [1] 1 3 4 5 6 8 9 10 11 12 13 14 15 16 17 18 #> #> [[16]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 #> #> [[17]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 #> #> [[18]] #> [1] 1 2 4 5 6 7 8 #> #> [[19]] #> [1] 1 2 3 4 6 7 8 9 10 11 #> #> [[20]] #> [1] 1 2 3 4 5 7 8 9 10 11 12 13 #> #> [[21]] #> [1] 1 2 3 4 5 7 8 9 10 11 12 #> #> [[22]] #> [1] 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 #> #> [[23]] #> [1] 1 2 3 4 5 7 8 9 10 11 #> #> [[24]] #> [1] 1 3 5 6 7 8 9 10 #> #> [[25]] #> [1] 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 #> #> [[26]] #> [1] 1 2 4 5 6 7 8 9 #> #> [[27]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 #> #> [[28]] #> [1] 1 2 3 4 5 6 7 8 9 10 11 #> #> [[29]] #> [1] 1 2 3 4 5 6 8 9 10 11 13 14 #> #> [[30]] #> [1] 1 2 3 4 6 7 8 9 10 11 12 #> #> [[31]] #> [1] 1 3 4 5 7 8 9 10 11 12 #> #> [[32]] #> [1] 1 2 3 4 5 6 7 8 9 10 #>