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
9
votes
1 answer

Is there a stringr equivalent for grep with value = TRUE?

Is there a stringr equivalent for grep with value set to TRUE? (I would like to avoid the NAs returned by the stringr command below.) library(stringr) x <- c("a", "b", "a") grep("a", x, value = TRUE) # returns "a" "a" str_extract(x, "a") # returns…
David Rubinger
  • 3,580
  • 1
  • 20
  • 29
9
votes
4 answers

Substring, Pad and Paste Columns in Dataframe without a Loop

I have this dataframe, it looks like this: I need to take the first character from the column at, the whole value in an, then put a counter on the end that increments for repeats in column an. This counter has to be always length of three. The end…
DieselBlue
  • 137
  • 6
9
votes
1 answer

Regex with non-capturing group using stringr in R

I am trying to use non-capturing groups with the str_extract function from the stringr package. Here is an example: library(stringr) txt <- "foo" str_extract(txt,"(?:f)(o+)") This returns "foo" while i expect it to return only "oo" like in this…
user3750030
  • 327
  • 3
  • 7
9
votes
4 answers

Unexpected behaviour with str_replace "NA"

I'm trying to convert a character string to numeric and have encountered some unexpected behaviour with str_replace. Here's a minimum working example: library(stringr) x <- c("0", "NULL", "0") # This works, i.e. 0 NA 0 as.numeric(str_replace(x,…
jkeirstead
  • 2,881
  • 3
  • 23
  • 26
8
votes
5 answers

Select every nth character from a string

I have a string of random letters with random spaces and some periods as well. I want to take every nth value (e.g. every 10th) from it. My thought was that if I can transpose it then I can use the row numbers to select for every nth value. Any help…
8
votes
4 answers

stringr str_replace on multiple patterns and replacements?

I tried > str_replace("abcdef", c("b", "d"), c(b="B", d="D")) [1] "aBcdef" "abcDef" hoping for [1] "aBcDef" How can we replace each pattern with a specific replacement with one function call to stringr::str_replace?
stevec
  • 41,291
  • 27
  • 223
  • 311
8
votes
5 answers

A Regex to remove digits except for words starting with #

I have some strings that can contain letters, numbers and '#' symbol. I would like to remove digits except for the words that start with '#' Here is an example: "table9 dolv5e #10n #dec10 #nov8e 23 hello" And the expected output is: "table dolve…
castaa95
  • 83
  • 4
8
votes
4 answers

Replace part of string with mutate (in a pipe)

I would like to replace a part of a string (between the first 2 underscores, the first group always being "i") like in the base R example below: library(dplyr) library(stringr) d <- tibble(txt = c("i_0000_GES", "i_0000_OISO", "i_0000_ASE1333"), …
r.user.05apr
  • 5,356
  • 3
  • 22
  • 39
8
votes
5 answers

Using str_extract in R to extract a number before a substring with regex

I would like to use str_extract in the stringr package to extract the numbers from strings in the form XX nights etcetc. I'm currently doing this: library(stringr) str_extract("17 nights$5 Days", "(\\d)+ nights") but that returns "17…
Harry M
  • 1,848
  • 3
  • 21
  • 37
8
votes
2 answers

str_extract_all: return all patterns found in string concatenated as vector

I want to extract everything but a pattern and return this concetenated in a string. I tried to combine str_extract_all together with sapply and cat x = c("a_1","a_20","a_40","a_30","a_28") data <- tibble(age = x) # extracting just the first…
MatzeKnop
  • 163
  • 1
  • 8
8
votes
1 answer

R, stringr - replace multiple characters from all elements of a vector with a single command

First, I am new to R and programming in general, so I apologise if this turns out to be a stupid question. I have a character vector similar to this: > vec <- rep(c("XabcYdef", "XghiYjkl"), each = 3) > vec [1] "XabcYdef" "XabcYdef" "XabcYdef"…
8
votes
1 answer

str_replace_all not working in pipeline

Here's my code: df <- df %>% filter(conditions x, y, and z) %>% str_replace_all(string, pattern, replacement) When doing this, I got the error: Error in str_replace_all(., string, pattern, replacement) : unused argument("") I know the code…
MokeEire
  • 638
  • 1
  • 8
  • 19
8
votes
3 answers

Extract a sample of words around a particular word using stringr in R

I've seen a couple of similar questions posted on SO regarding this topic, but they seem to be worded improperly (example) or in a different language (example). In my scenario, I consider everything that is surrounded by white space to be a word.…
tblznbits
  • 6,602
  • 6
  • 36
  • 66
8
votes
5 answers

Extract the last word between | |

I have the following dataset > head(names$SAMPLE_ID) [1] "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Moraxellaceae|Acinetobacter|" [2] "Bacteria|Firmicutes|Bacilli|Bacillales|Bacillaceae|Bacillus|" [3]…
Keniajin
  • 1,649
  • 2
  • 20
  • 43
8
votes
3 answers

Why is stringr changing encoding when manipulating strings?

There is this strange behavior of stringr, which is really annoying me. stringr changes without a warning the encoding of some strings that contain exotic characters, in my case ø, å, æ, é and some others... If you str_trim a vector of characters,…
Arthur
  • 1,208
  • 13
  • 25