left: Return text to the left and the right of a string

View source: R/left_and_right.R

leftR Documentation

Return text to the left and the right of a string

Description

left takes a string and a character as arguments, then returns the characters to the left. right takes a string and a character as arguments, then returns the characters to the right.

Wrappers around some regular expressions. Left and right operate similar to combining left()/right() and search() in excel. So maybe there's a bit of nostalgia for me in using these functions. But I also think that the naming makes it clearer what is going on.

Usage

left(string, char)

right(string, char)

Arguments

string

The string

char

A character used to delimit the string

Value

A string

References

https://stackoverflow.com/questions/45070117/r-check-if-special-character-in-string

https://stackoverflow.com/questions/36928870/r-check-if-string-contains-special-characters

https://stackoverflow.com/questions/14836754/is-there-an-r-function-to-escape-a-string-for-regex-characters{}

Examples

a <- c("left.right","left.right","left.right")
left(a, ".")
right(a, ".")

library(dplyr)
library(tibble)

test_df <- tibble::tibble(
  label = c("resection_margin=Mohs, > 1 cm",
            "resection_margin=Mohs, > 1 cm",
            "resection_margin=Mohs, <= 1 cm",
            "  resection_margin=Mohs, > 1 cm"))
test_df %>%
  mutate(left = left(label, "="),
         right = right(label, "="))

right(test_df$label, "=")
left(test_df$label, "=")
stringr::str_trim(left(test_df$label, "="), side = "both")


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.