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

gsub() in R is not replacing '.' (dot)

I want to replace dots in "2014.06.09" to "2014-06-09". I am using gsub() function for it. If x <- "2014.06.09" gsub('2', '-' ,x) # [1] "-014.06.09" But when I try gsub('.', '-', x) # [1] "----------" instead of "2014-06-09". class(x) #…
Zeeshan
  • 1,248
  • 1
  • 12
  • 19
82
votes
4 answers

Extracting a string between other two strings in R

I am trying to find a simple way to extract an unknown substring (could be anything) that appear between two known substrings. For example, I have a string: a<-" anything goes here, STR1 GET_ME STR2, anything goes here" I need to extract the string…
Sasha
  • 5,783
  • 8
  • 33
  • 37
54
votes
5 answers

How to ignore case when using str_detect?

stringr package provides good string functions. To search for a string (ignoring case) one could use stringr::str_detect('TOYOTA subaru',ignore.case('toyota')) This works but gives warning Please use (fixed|coll|regex)(x, ignore_case = TRUE)…
userJT
  • 11,486
  • 20
  • 77
  • 88
38
votes
4 answers

How to extract everything until first occurrence of pattern

I'm trying to use the stringr package in R to extract everything from a string up until the first occurrence of an underscore. What I've tried str_extract("L0_123_abc", ".+?(?<=_)") > "L0_" Close but no cigar. How do I get this one? Also, Ideally…
Ben
  • 20,038
  • 30
  • 112
  • 189
36
votes
4 answers

Splitting a dataframe string column into multiple different columns

What I am trying to accomplish is splitting a column into multiple columns. I would prefer the first column to contain "F", second column "US", third "CA6" or "DL", and the fourth to be "Z13" or "U13" etc etc. My entire df follows the same pattern…
Tim
  • 776
  • 3
  • 8
  • 15
35
votes
2 answers

regex multiple pattern with singular replacement

I am trying to replace both "st." and "ste." with "st". Seems like the following should work but it does not: require("stringr") county <- c("st. landry", "ste. geneveve", "st. louis") str_replace_all(county, c("st\\.", "ste\\."), "st")
MikeTP
  • 7,716
  • 16
  • 44
  • 57
33
votes
4 answers

dplyr: inner_join with a partial string match

I'd like to join two data frames if the seed column in data frame y is a partial match on the string column in x. This example should illustrate: # What I have x <- data.frame(idX=1:3, string=c("Motorcycle", "TractorTrailer", "Sailboat")) y <-…
Stephen Turner
  • 2,574
  • 8
  • 31
  • 44
28
votes
2 answers

How to escape a backslash in R?

I'm working in R and having troubles escaping the backslash. I am using the library stringr. install.packages("stringr", repos='http://cran.us.r-project.org') library("stringr") I would like to do str = str_replace_all(str, "\", "") So I tried str…
Paul Fournel
  • 10,807
  • 9
  • 40
  • 68
27
votes
2 answers

Non-greedy string regular expression matching

I'm pretty sure I'm missing something obvious here, but I cannot make R to use non-greedy regular expressions: > library(stringr) > str_match('xxx aaaab yyy', "a.*?b") [,1] [1,] "aaaab" Base…
Victor K.
  • 4,054
  • 3
  • 25
  • 38
22
votes
4 answers

Split a character vector into individual characters? (opposite of paste or stringr::str_c)

An incredibly basic question in R yet the solution isn't clear. How to split a vector of character into its individual characters, i.e. the opposite of paste(..., sep='') or stringr::str_c() ? Anything less clunky than this: sapply(1:26, function(i)…
smci
  • 32,567
  • 20
  • 113
  • 146
20
votes
3 answers

remove leading 0s with stringr in R

I have the following data id 00001 00010 00022 07432 I would like to remove the leading 0s so the data would like like the following id 1 10 22 7432
Alex
  • 2,603
  • 4
  • 40
  • 73
18
votes
5 answers

Subset string by counting specific characters

I have the following strings: strings <- c("ABBSDGNHNGA", "AABSDGDRY", "AGNAFG", "GGGDSRTYHG") I want to cut off the string, as soon as the number of occurances of A, G and N reach a certain value, say 3. In that case, the result should…
Nivel
  • 629
  • 4
  • 12
18
votes
7 answers

Regex to detect if all alphabetic characters are upper case

How would I go about detecting if all alphabetic characters in a string (of >= 2 characters) are upper case? Ultimately, I'm trying to filter out chapter title names, that are rows in my data-set. So if a chapter title is "ARYA", I want that…
Evan O.
  • 1,553
  • 2
  • 11
  • 20
18
votes
3 answers

stringr str_extract capture group capturing everything

I'm looking to extract the year from a string. This always comes after an 'X' and before "." then a string of other characters. Using stringr's str_extract I'm trying the following: year = str_extract(string =…
Preston
  • 7,399
  • 8
  • 54
  • 84
18
votes
2 answers

Get last element from str_split

I have a R list of strings and I want to get the last element of each require(stringr) string_thing <- "I_AM_STRING" Split <- str_split(string_thing, "_") Split[[1]][length(Split[[1]])] but how can I do this with a list of…
KillerSnail
  • 3,321
  • 11
  • 46
  • 64
1
2 3
99 100