Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
#' @title Subset a List from a Matched String | ||
#' | ||
#' @description | ||
#' This function takes a list of strings and a key string as input. It returns a new list that starts from the first occurrence of the key string in the original list and includes all the strings that follow it. If the key string is not found in the list, the function returns `NULL`. | ||
#' | ||
#' | ||
#' @description This function takes a list of strings and a key string as input. | ||
#' It returns a new list that starts from the first occurrence of the key | ||
#' string in the original list and includes all the strings that follow it. If | ||
#' the key string is not found in the list, the function returns `NULL`. | ||
#' Optional argument `before` allows the logic to be reversed, returning a new | ||
#' list that start from the first value in the original list up to the matched | ||
#' string. | ||
#' | ||
#' @param x A character vector to perform the matching and subsetting on. | ||
#' @param key A character string to match in `x`. | ||
#' | ||
#' @return A character vector starting from the first occurrence of `key` in `x` and including all subsequent elements. If `key` is not found in `x`, the function returns `NULL`. | ||
#' | ||
#' @param before A logical value indicating whether the strings _before_ the | ||
#' matched key should be returned. Defaults to FALSE. If set to TRUE, strings | ||
#' _before_ the matched key are returned. | ||
#' | ||
#' @return By default, a character vector starting from the first occurrence of | ||
#' `key` in `x` and including all subsequent elements. Logic can be reversed | ||
#' with `before` argument. If `key` is not found in `x`, the function returns | ||
#' `NULL`. | ||
#' | ||
#' @examples | ||
#' x <- c("a", "b", "c", "d", "e") | ||
#' key <- "c" | ||
#' subset_from_match(x, key) # Returns: "c" "d" "e" | ||
#' | ||
#' | ||
#' @export | ||
subset_from_match <- function(x, key) { | ||
subset_from_match <- function(x, key, before = FALSE) { | ||
index <- match(key, x) | ||
if (!is.na(index)) { | ||
return(x[index:length(x)]) | ||
if (!is.na(index) & !before) { | ||
x[index:length(x)] | ||
} else if (!is.na(index) & before){ | ||
x[1:index] # up to the matched key | ||
} else { | ||
return(NULL) | ||
NULL | ||
} | ||
} |