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
16
votes
3 answers

count the number of occurrences of "(" in a string

I am trying to get the number of open brackets in a character string in R. I am using the str_count function from the stringr package s<- "(hi),(bye),(hi)" str_count(s,"(") Error in stri_count_regex(string, pattern, opts_regex = attr(pattern, …
NPHARD
  • 197
  • 1
  • 2
  • 5
16
votes
4 answers

Detect multiple strings with dplyr and stringr

I'm trying to combine dplyr and stringr to detect multiple patterns in a dataframe. I want to use dplyr as I want to test a number of different columns. Here's some sample data: test.data <- data.frame(item = c("Apple", "Bear", "Orange", "Pear",…
r.bot
  • 5,309
  • 1
  • 34
  • 45
16
votes
3 answers

str_replace (package stringr) cannot replace brackets in r?

I have a string, say fruit <- "()goodapple" I want to remove the brackets in the string. I decide to use stringr package because it usually can handle this kind of issues. I use : str_replace(fruit,"()","") But nothing is replaced, and the…
nan
  • 1,131
  • 4
  • 18
  • 35
15
votes
2 answers

Filter by multiple patterns with filter() and str_detect()

I would like to filter a dataframe using filter() and str_detect() matching for multiple patterns without multiple str_detect() function calls. In the example below I would like to filter the dataframe df to show only rows containing the letters a f…
user6571411
  • 2,749
  • 4
  • 16
  • 29
15
votes
5 answers

Remove all text between two brackets

Suppose I have some text like this, text<-c("[McCain]: We need tax policies that respect the wage earners and job creators. [Obama]: It's harder to save. It's harder to retire. [McCain]: The biggest problem with American healthcare system is that it…
Michael Davidson
  • 1,391
  • 1
  • 14
  • 31
13
votes
2 answers

What is the difference between paste/paste0 and str_c?

I don't seem to see a difference between paste/paste0 and str_c for combining a single vector into a single string, multiple strings into one string, or multiple vectors into a single string. While I was writing the question I found this:…
Ben G
  • 4,148
  • 2
  • 22
  • 42
12
votes
4 answers

How to replace characters in a string one at a time generating new string for each replacement?

I have a vector of strings c("YSAHEEHHYDK", "HEHISSDYAGK", "TFAHTESHISK", "ISLGEHEGGGK", "LSSGYDGTSYK", "FGTGTYAGGEK", "VGASTGYSGLK", "TASGVGGFSTK", "SYASDFGSSAK", "LYSYYSSTESK") for each string I would like to replace "Y", "S" or "T" with "pY",…
Pavel Shliaha
  • 773
  • 5
  • 16
12
votes
2 answers

Is there an R function equivalent to range in Python?

I am looking to split a string into ngrams of 3 characters - e.g HelloWorld would become "Hel", "ell", "llo", "loW" etc How would I achieve this using R? In Python it would take a loop using the range function - e.g. [myString[i:] for i in…
Brisbane Pom
  • 521
  • 7
  • 18
12
votes
1 answer

str_replace doesn't replace all occurrences, but gsub does?

I am trying to remove brackets from a string like the one below. library(stringr) x <- "(Verhoeff,1937)" str_replace(string = x, pattern = "(\\()|(\\))", replacement = "") [1] "Verhoeff,1937)" gsub(pattern = "(\\()|(\\))", replacement = "", x =…
zankuralt
  • 173
  • 2
  • 9
12
votes
2 answers

stringr equivalent to grep

Is there an stringr equivalent to base R's grep function? I want to have the index of the string that matches. Example: grep("F|Y", LETTERS) [1] 6 25 With stringr my workaround would be using which as follows: which(str_detect(LETTERS, "F|Y")) [1]…
Rentrop
  • 20,979
  • 10
  • 72
  • 100
12
votes
5 answers

Installation of packages ‘stringr’ and ‘stringi’ had non-zero exit status

Please help me to install stringr and stringi packages in R. The result is: install.packages("stringi") Installing package into ‘C:/Users/kozlovpy/Documents/R/win-library/3.2’ (as ‘lib’ is unspecified) пробую URL…
Pavel Kozlov
  • 131
  • 1
  • 1
  • 4
12
votes
4 answers

Remove URLs from string

I have a vector of strings—myStrings—in R that look something like: [1] download file from `http://example.com` [2] this is the link to my website `another url` [3] go to `another url` from more info. where another url is a valid http url but…
Tavi
  • 2,668
  • 11
  • 27
  • 41
11
votes
3 answers

fuzzy version of stringr::str_detect for filtering dataframe

I've got a database with free text fields that I want to use to filter a data.frame or tibble. I could perhaps with lots of work create a list of all possible misspellings of my search terms that currently occur in the data (see example of all the…
11
votes
2 answers

How do I split a string with tidyr::separate in R and retain the values of the separator string?

I have a data set: crimes<-data.frame(x=c("Smith", "Jones"), charges=c("murder, first degree-G, manslaughter-NG", "assault-NG, larceny, second degree-G")) I'm using tidyr:separate to split the charges column on a match with…
TDog
  • 165
  • 1
  • 2
  • 9
11
votes
2 answers

Counting consecutive patterns in strings using R

I'm attempting to write a function to count the number of consecutive instances of a pattern. As an example, I'd like the string string<-"A>A>A>B>C>C>C>A>A" to be transformed into "3 A > 1 B > 3 C > 2 A" I've got a function that counts the…