Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions R/select.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' Keep only strings matching a pattern.
#'
#' Vectorised over \code{string} only.
#'
#' @param string input vector. This must be an atomic vector, and will be
#' coerced to a character vector
#' @param pattern pattern to look for, as defined by a POSIX regular
#' expression. See the ``Extended Regular Expressions'' section of
#' \code{\link{regex}} for details. See \code{\link{fixed}},
#' \code{\link{ignore.case}} and \code{\link{perl}} for how to use other
#' types of matching: fixed, case insensitive and perl-compatible.
#' @return string vector
#' @seealso \code{\link{grep}} with argument \code{value = TRUE},
#' \code{\link{str_detect}} for a version returning booleans for matches.
#' @keywords character
#' @export
#' @examples
#' fruit <- c("apple", "banana", "pear", "pinapple")
#' str_select(fruit, "a")
#' str_select(fruit, "^a")
#' str_select(fruit, "a$")
#' str_select(fruit, "b")
#' str_select(fruit, "[aeiou]")
#'
str_select <- function(string, pattern) {
if (length(pattern) > 1) {
stop("Only one pattern can be used to select within a string")
}
string[which(str_detect(string, pattern))]
}