1- # ' Keep strings matching a pattern.
1+ # ' Keep strings matching a pattern, or find positions.
2+ # '
3+ # ' \code{str_subset()} is a wrapper around \code{x[str_detect(x, pattern)]},
4+ # ' and is equivalent to \code{grep(pattern, x, value = TRUE)}.
5+ # ' \code{str_which()} is a wrapper around \code{which(str_detect(x, pattern))},
6+ # ' and is equivalent to \code{grep(pattern, x)}.
27# '
3- # ' This is a convenient wrapper around \code{x[str_detect(x, pattern)]}.
48# ' Vectorised over \code{string} and \code{pattern}
59# '
610# ' @inheritParams str_detect
1115# ' @examples
1216# ' fruit <- c("apple", "banana", "pear", "pinapple")
1317# ' str_subset(fruit, "a")
18+ # ' str_which(fruit, "a")
19+ # '
1420# ' str_subset(fruit, "^a")
1521# ' str_subset(fruit, "a$")
1622# ' str_subset(fruit, "b")
1723# ' str_subset(fruit, "[aeiou]")
1824# '
19- # ' # Missings are silently dropped
25+ # ' # Missings never match
2026# ' str_subset(c("a", NA, "b"), ".")
27+ # ' str_which(c("a", NA, "b"), ".")
2128str_subset <- function (string , pattern ) {
2229 switch (type(pattern ),
2330 empty = ,
@@ -27,3 +34,9 @@ str_subset <- function(string, pattern) {
2734 regex = stri_subset_regex(string , pattern , omit_na = TRUE , opts_regex = opts(pattern ))
2835 )
2936}
37+
38+ # ' @export
39+ # ' @rdname str_subset
40+ str_which <- function (string , pattern ) {
41+ which(str_detect(string , pattern ))
42+ }
0 commit comments