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
1
vote
1 answer

change several column names() in data.frame() with str_replace_all()

I read this this question and practiced matching patterns, but I am still not figuring it. I have a panel with the same measure, several times per year. Now, I want to rename them in a logical way. My raw data looks a bit like…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
1
vote
1 answer

Extracting Data and creating a new column of repetitive values

I'm gathering some data using a script like this: library(tidyverse) library(rvest) library(magrittr) library(stringr) foo_matrix <- read_html("#address") test <- foo_matrix %>% html_nodes(#Retrieval Information) %>% html_text()…
user9302275
1
vote
1 answer

Using readr to read TSV where some fields have quotes. After writing back out, there is an extra set of quotes

I have a TSV where one of the fields is a string representation of an array, it looks something like this: A B 1 ["hello", "to", "you"] 2 ["some"] 3 ["stuff", "blah"] I'm trying to read it in using readr library(readr) df =…
divide_by_zero
  • 997
  • 1
  • 8
  • 20
1
vote
0 answers

How do I manipulate an unstructured text data using stringr?

I have an unstructured data which I hope to manipulate it so that it becomes a data that is usable for analysis. The following is my dataframe which contains 97 observations and 5 variables, namely, DRUG_NAME, DRUG_STRENGTH_NO, DRUG_STRENGTH_UNIT,…
HNSKD
  • 1,614
  • 2
  • 14
  • 25
1
vote
2 answers

How do I split strings into number and the remaining string using stringr in r?

I would like to split strings in my dataframe using stringr. The following is my dataframe: df<-data.frame(ID = 1:26, DRUG_STRENGTH = c("50 MG", "1250 MG", "20 MG", "200 MG", "2MG", "60MG", NA, "300IU", NA,…
HNSKD
  • 1,614
  • 2
  • 14
  • 25
1
vote
1 answer

Split String by Upper Case Consisting of Both Latin and Unicode

Building on the Splitting String based on letters case answer; lang <- "DeutschEsperantoItalianoNederlandsNedersaksiesNorskРусский" strsplit(lang, "(?!^)(?=[[:upper:]])", perl = T) results in "Deutsch" "Esperanto" "Italiano" …
user6550364
1
vote
3 answers

R Regex to identify and replace characters between multiple dots

I have the following codes "ABC.A.SVN.10.10.390.10.UDGGL" "XYZ.Z.SVN.11.12.111.99.ASDDL" and I need to replace the characters that exist between the 2nd and the 3rd dot. In this case it is SVN but it may well be any combination of between A and…
J. Doe.
  • 1,255
  • 1
  • 12
  • 25
1
vote
2 answers

Subtracting strings in R

Is there a simple way to substract strings across columns in a tibble or data.frame? For example in the tibble below, is there a way to easily create column b from columns a and c? Similar to how I create c from a and b? (ie c = a + b, so b = c -…
ColinTea
  • 998
  • 1
  • 9
  • 15
1
vote
2 answers

How to extract substrings in R using stringr::str_match

I have the following two strings: x <- "chr1:625000-635000.BB_162.Adipose" y <- "chr1:625000-635000.BB_162.combined.HMSC-ad" With this regex I have no problem capturing parts of x > stringr::str_match(x,"(\\w+):(\\d+)-(\\d+)\\.(\\w+)\\.(\\w+)") …
pdubois
  • 7,640
  • 21
  • 70
  • 99
1
vote
1 answer

Use ifelse function when extracting data from pdfs

I'm trying to extract part of a PDF document. There are different car models with pricing and when a document uses a lowercase instead of an uppercase letter, I don't know how to extract the dollar amount. I think an ifelse statement would work fine…
Austin
  • 153
  • 2
  • 11
1
vote
2 answers

Partial regex results in R

Would like to know the error while using str_replace_all while doing a transformation on a string: abc <- "Good Product ...but it's darken the skin tone..why...?" I would like to do an additional manipulation in order to enable convert it to…
AJosh
  • 11
  • 4
1
vote
0 answers

Using stringr in R - number string

I want to subset a data table (DS) based on a column (Col) with the format "00000000000000123456" using regular expressions in R. Numbers that meet this format should be output to DS_Correct and those that do not match should be output to DS_Error.…
Neku
  • 23
  • 3
1
vote
2 answers

Generating new columns in an R dataframe based on applying a function across multiple columns

What I'd like to do is apply a function to multiple columns in a dataframe, recording the output as a new column. To make this clearer, I'd like to take a dataframe of the form: first_name last_name age Alice Smith 45 Bob …
anthr
  • 1,026
  • 4
  • 17
  • 34
1
vote
0 answers

Limiting word count in a character column in R and saving extra words in another variable

We are moving data from an old ERP system to a new one, the new one limits the description of items to 50 words, while the older one had no such limit. How can I ask R to remove the words at the end of the string if they are more than 50? Edit: The…
pyeR_biz
  • 986
  • 12
  • 36
1
vote
2 answers

Replace multiple values using a reference table

I’m cleaning a data base, one of the fields is “country” however the country names in my data base do not match the output I need. I though of using str_replace function but I have over 50 countries that need to be fix, so it’s not the most…
Claudia
  • 105
  • 2
  • 7