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

process destination string from log file to extract relevant data?

I'm trying to analyse a log file from nginx Particularly, i want to add to my data frame the first thing after the first / For example: df1 <- structure(data.frame(V5 = c("GET /SOMETHING1/__assets__/shiny-server.js HTTP/1.1", …
Ignacio
  • 7,646
  • 16
  • 60
  • 113
1
vote
1 answer

grep RNA parenthesis representation with R

I am seeking to extract every time an open parenthesis + any number of dots + close parenthesis happens within my string. The following is my R code: pos_test <- "..((((............))))))))))....((........))(((....)))..." pos_test_matrix <-…
laemtao
  • 147
  • 11
1
vote
3 answers

Replace Value & Shift Data Frame If Certain Condition Met

I've scraped data from a source online to create a data frame (df1) with n rows of information pertaining to individuals. It comes in as a single string, and I split the words apart into appropriate columns. 90% of the information is correctly…
wetcoaster
  • 367
  • 3
  • 15
1
vote
2 answers

Matching special character in R

Hi I have the following data. shopping_list <- c("apples x4", "bag of flour", "bag of sugar", "milk x2", "appple+20gfree", "BELI HG MSWAT ALA +VAT T 100g BAR WR", "TOOLAIT CASSE+LSST+SSSRE…
1
vote
3 answers

Non-capturing Group in R Regex

I'm trying to extract the nth word from strings and found several links that suggest a method that doesn't seem to work in R. myString <- "HANS CHRISTIAN ANDERSON III" str_extract(myString,'(?:\\S+ ){1}(\\S+)') # [1] "HANS…
jks612
  • 1,224
  • 1
  • 11
  • 20
1
vote
2 answers

R: Extract last N words from character column in data.table

I was hoping for some help with extracting the last N words from a column in a data.table.. and then assigning it to a new column. test <- data.table(original = c('the green shirt totally brings out your eyes' ,…
AlexP
  • 577
  • 1
  • 4
  • 15
1
vote
2 answers

extract a pattern from a text in R from a subset of patterns

I have list of codes as…
Shankar Pandala
  • 969
  • 2
  • 8
  • 28
1
vote
1 answer

finding numeric pattern in character vector containing numeric digits separated by space

I got a data frame which containg a column containing codes separated by space. And I want to search those ids by applying "<" or ">" etc arithmatic operaters. Input data frame: df <- data.frame(Id=c(101, 102,103), Codes=c("1 2 3", "2 4 5", "4…
indra_patil
  • 283
  • 1
  • 4
  • 11
1
vote
1 answer

Changing a pattern that occurs multiple times in a string in R

I have a dataframe with one column, where each row represents part of a sql select statement, for example below: test <- bind_rows( data.frame(text = "spend_1 + spend_2", stringsAsFactors = FALSE), data.frame(text = "spend_1 + spend_2 +…
Sam Gilbert
  • 1,642
  • 3
  • 21
  • 38
1
vote
2 answers

stringr: U_REGEX_BAD_INTERVAL error

I have a regular expression that is correctly parsed by grepl but generates an error when used as pattern of str_extract_all. I'm using stringr v1.0.0, R v3.2.3 under OSX. In this question a regex passed to a stringr generates a similar error, yet…
CptNemo
  • 6,455
  • 16
  • 58
  • 107
1
vote
1 answer

Cleaning 'stringr str_replace_all' automatic concatenation when matching multiple times

I used police_officer <- str_extract_all(txtparts, "ID:.*\n") to extract all the names of the police officers involved in a 911 call from a text file. example: 2237 DISTURBANCE Report taken Call Taker: Telephone Operators Sharon L…
Jomisilfe
  • 55
  • 1
  • 9
1
vote
2 answers

Regex extraction in R

I'm working on a project which uses Multidimensional Scaling to try and group politicians together based on voting records. My goodness of fit is high; however, I want to plot the MDS coordinates with the names of the politicians so I can draw…
1
vote
2 answers

How to add total number of same string with new column in data matrix with R

Suppose I have a matrix, 5 by 5 with fruit names (5 class fruits). I want to add 5 new columns in this existing matrix with the total number of single fruits in each of the rows, and finally one extra row to show the summation of each same kind of…
bim
  • 612
  • 7
  • 18
1
vote
1 answer

How to match a word after a certain character with out knowing the word in R?

I would to like to match the word after a - in my text then if that matched word is the end of another word then I would like to do a split between the word and the matched word. Example of the text: JOHN LION - XYZ RAN RUN TREEABC GRASS - ABC LIMB…
Dre
  • 713
  • 1
  • 8
  • 27
1
vote
3 answers

Create a new column that counts the number of a sub-string in a string column?

New to R here. I have a problem to solve: I need to create some new columns that count 1 if a sub-string appears one or more times in a string column. Like this: Existing Column New Col (True if apple) New Col (True if banana) apple,…
gogolaygo
  • 199
  • 1
  • 12