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

Get text from href tag after specific class

I am trying to scrape a webpage library(RCurl) webpage <- getURL("https://somewebpage.com") webpage
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
7
votes
2 answers

R - stringr add newline character every two spaced digits

Given str1 <- "0 1 1 2 2 3 3 4 0 4" I want: str2 <- "0 1\n1 2\n2 3\n3 4\n0 4" What's the way to do this with stringr?
andandandand
  • 21,946
  • 60
  • 170
  • 271
7
votes
4 answers

str_replace_all replacing named vector elements iteratively not all at once

Let's say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I'd like to use stringr::str_replace_all to replace certain letters with others. According to the documentation, str_replace_all can take a named vector and…
biomiha
  • 1,358
  • 2
  • 12
  • 25
7
votes
2 answers

What does the error "the condition has length > 1 and only the first element will be used" mean?

Here is my data set: FullName <- c("Jimmy John Cephus", "Frank Chester", "Hank Chester", "Brody Buck Clyde", "Merle Rufus Roscoe Jed Quaid") df <- data.frame(FullName) Goal: Look into FullName for any spaces, " ", and extract out the FirstName. My…
G-Bruce
  • 83
  • 1
  • 1
  • 7
7
votes
2 answers

How to replace string for every row in specfic column using dplyr and stringr

I have the following tibble: library(tidyverse) df <- tibble::tribble( ~sample, ~colB, ~colC, "foo", 1, 2, "bar_x", 2, 3, "qux.6hr.ID", 3, 4, "dog", 1, 1 ) df #> # A tibble: 4 x 3 #> sample colB colC #> …
pdubois
  • 7,640
  • 21
  • 70
  • 99
7
votes
1 answer

Detect a list of words in a string variable and extract matched words to a new variable in data frame

I have a two variable dataframe one of which is a character vector. Each row in "MyVector" contains a string with exactly one name (i.e. "Pete"). The name can vary in its location in the character string. I want to create code that will match the…
RareAir
  • 135
  • 1
  • 2
  • 5
7
votes
3 answers

extract number after specific string

I need to find the number after the string "Count of". There could be a space or a symbol between the "Count of" string and the number. I have something that works on www.regex101.com but does not work with stringr str_extract…
Matthew Crews
  • 4,105
  • 7
  • 33
  • 57
7
votes
3 answers

How to remove + (plus sign) from string in R?

Say I use gsub and want to remove the following (=,+,-) sign from the string and replace with an underscore. Can someone describe what is going on when I try to use the gsub with a plus sign (+). test<- "sandwich=bread-mustard+ketchup" # [1]…
Jonathan
  • 611
  • 2
  • 7
  • 15
7
votes
2 answers

Cannot load package in R

Since yesterday, I cannot load some common used packages, e.g. reshape2 and stringr through library command. Step to reproduce my problem: 1) Start a new session in Rstudio server (Version 0.99.467). I also kill the active-sessions in Rstudio…
Bangyou
  • 9,462
  • 16
  • 62
  • 94
7
votes
2 answers

Extract text in parentheses in R

Two related questions. I have vectors of text data such as "a(b)jk(p)" "ipq" "e(ijkl)" and want to easily separate it into a vector containing the text OUTSIDE the parentheses: "ajk" "ipq" "e" and a vector containing the text INSIDE the…
user2817329
  • 83
  • 1
  • 5
7
votes
3 answers

Regular Expression in Base R Regex to identify email address

I am trying to use the stringr library to extract emails from a big, messy file. str_match doesn't allow perl=TRUE, and I can't figure out the escape characters to get it to work. Can someone recommend a relatively robust regex that would work in…
toomey8
  • 333
  • 3
  • 8
7
votes
3 answers

Speed up `strsplit` when possible output are known

I have a large data frame with a factor column that I need to divide into three factor columns by splitting up the factor names by a delimiter. Here is my current approach, which is very slow with a large data frame (sometimes several million…
Noam Ross
  • 5,969
  • 5
  • 24
  • 40
6
votes
6 answers

How to detect range of positions of specific set of characters in a string

I have the following sequence: my_seq <- "----?????-----?V?D????-------???IL??A?---" What I want to do is to detect range of positions of non-dashed characters. ----?????-----?V?D????-------???IL??A?--- | | | | | | | 1 …
littleworth
  • 4,781
  • 6
  • 42
  • 76
6
votes
1 answer

Different behavior of base R gsub and stringr::str_replace_all?

I would expect gsub and stringr::str_replace_all to return the same result in the following, but only gsub returns the intended result. I am developing a lesson to demonstrate str_replace_all so I would like to know why it returns a different result…
qdread
  • 3,389
  • 19
  • 36
6
votes
3 answers

How to subtract two comma separated columns in R?

I have a small problem that I can't seem to solve. Given two columns: dt <- data.table(ColumnA = c("A,B,C,A,A,A", "A,B,C"), ColumnB = c("A,C,A", "C")) I would like to "subtract" columnB from columnA, which will result in: data.table(Result =…
Snowflake
  • 2,869
  • 3
  • 22
  • 44