Questions tagged [stringr]

The stringr package is a wrapper for the R stringi package that provides consistent function names and error handling for string manipulation. It is part of the Tidyverse collection of packages. Use this tag for questions involving the manipulation of strings specifically with the stringr package. For general R string manipulation questions use the R tag together with the generic string tag.

's stringr package provides a more consistent user interface to base-R's string manipulation and regular expression functions.

Repositories

Other resources

Related tags

2501 questions
6
votes
1 answer

detect string with both AND and OR boolean operator in R

I have a text like this: text = 'I love apple, pear, grape and peach' If I want to know if the text contain either apple or pear. I can do the following and works fine: str_detect(text,"apple|pear") [1] TRUE my question is what if I want to use…
zesla
  • 11,155
  • 16
  • 82
  • 147
6
votes
2 answers

How can I extract and remove a string? So I can have similar expressions match 1 time instead of multiple times

Problem description: I'm currently extracting names from a book series. Many characters will go by nicknames, parts of names, or titles. I have a list of names that I'm using as a pattern on all of the data. The problem is that I'm getting multiple…
6
votes
1 answer

Filtering on a Stringr Match (str_detect) EXCEPT For a Particular Similar Value in R?

I'm trying to create a dplyr pipeline to filter on Imagine a data frame jobs, where I want to filter out the most-senior positions from the titles column: titles Chief Executive Officer Chief Financial Officer Chief Technical…
alxlvt
  • 675
  • 2
  • 10
  • 18
6
votes
3 answers

changing all values in one column in a filtered data.frame in R

I have a very messy data frame, with one column with values that are understandable to humans but not to computers, a bit like the one below. df<-data.frame("id"=c(1:10), "colour"=c("re d", ", red", "re-d","green", "gre, en", ",…
Mactilda
  • 393
  • 6
  • 18
6
votes
1 answer

Difference between `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`, `stri_paste`?

What are the differences between all of these functions that seem very similar ?
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
6
votes
2 answers

Split a string AFTER a pattern occurs

I have the following vector of strings. It contains two elements. Each of the elements is composed by two collapsed phrases. strings <- c("This is a phrase with a NameThis is another phrase", "This is a phrase with the number 2019This is…
allanvc
  • 1,096
  • 10
  • 23
6
votes
5 answers

If else statements to check if a string contains a substring in R

I have a list that contains multiple strings for each observation (see below). [1] A, C, D [2] P, O, E [3] W, E, W [4] S, B, W I want to test if the strings contain certain substrings and if so, return the respective substring, in this…
Carolin
  • 539
  • 2
  • 7
  • 15
6
votes
2 answers

R / stringr: split string, but keep the delimiters in the output

I tried to search for the solution, but it appears that there is no clear one for R. I try to split the string by the pattern of, let's say, space and capital letter and I use stringr package for that. x <- "Foobar foobar, Foobar…
perechen
  • 125
  • 9
6
votes
3 answers

How to use `stringr` in `dplyr` pipe

I am having trouble with this code which attempts to edit some strings in a dplyr pipe. Here is some data that throws the following error. Any ideas? data_frame(id = 1:5, name = c('this and it pretty long is a', 'name…
elliot
  • 1,844
  • 16
  • 45
6
votes
2 answers

Extract first sentence in string

I want to extract the first sentence from following with regex. The rule I want to implement (which I know won't be universal solution) is to extract from string start ^ up to (including) the first period/exclamation/question mark that is preceded…
geotheory
  • 22,624
  • 29
  • 119
  • 196
6
votes
5 answers

str_extract: Extracting exactly nth word from a string

I know this question has been asked at several places, but I didnt see a precise answer to this. So I am trying to extract exactly the 2nd word from a string("trying to") in R with the help of regex. I do not want to use unlist(strsplit) sen= "I…
Manu Sharma
  • 1,593
  • 4
  • 25
  • 48
6
votes
2 answers

Why does is this end of line (\\b) not recognised as word boundary in stringr/ICU and Perl

Using stringr i tried to detect a € sign at the end of a string as follows: str_detect("my text €", "€\\b") # FALSE Why is this not working? It is working in the following cases: str_detect("my text a", "a\\b") # TRUE - letter instead of…
Rentrop
  • 20,979
  • 10
  • 72
  • 100
6
votes
2 answers

R - Find all vector elements that contain all strings / patterns - str_detect grep

Sample data files.in.path = c("a.4.0. name 2015 - NY.RDS", "b.4.0. name 2016 - CA.RDS", "c.4.0. name 2015 - PA.RDS") strings.to.find = c("4.0", "PA") I want the logical vector that shows all elements that…
LWRMS
  • 547
  • 8
  • 18
6
votes
3 answers

How to use stringr's replace_all() function to replace specific matches in a string

The stringr package has helpful str_replace() and str_replace_all() functions. For example mystring <- "one fish two fish red fish blue fish" str_replace(mystring, "fish", "dog") # replaces the first occurrence str_replace_all(mystring, "fish",…
Ben
  • 20,038
  • 30
  • 112
  • 189
6
votes
5 answers

Extracting a number following specific text in R

I have a data frame which contains a column full of text. I need to capture the number (can potentially be any number of digits from most likely 1 to 4 digits in length) that follows a certain phrase, namely 'Floor Area' or 'floor area'. My data…
RichS
  • 659
  • 12
  • 19