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
6
votes
4 answers

Create new variables based upon specific values

I read up on regular expressions and Hadley Wickham's stringr and dplyr packages but can't figure out how to get this to work. I have library circulation data in a data frame, with the call number as a character variable. I'd like to take the…
Concept Delta
  • 187
  • 2
  • 10
6
votes
2 answers

Identify continuously occurring stretch of specific letters in a string using R

I would like to identify if the string column in the data frame below repeats the letters "V" or "G" at least 5 times within the first 20 characters of the string. Sample data: data = data.frame(class = c('a','b','C'), string = …
Veerendra Gadekar
  • 4,452
  • 19
  • 24
6
votes
3 answers

Sequentially replace multiple places matching single pattern in a string with different replacements

Using stringr package, it is easy to perform regex replacement in a vectorized manner. Question: How can I do the following: Replace every word in hello,world??your,make|[]world,hello,pos to different replacements, e.g. increasing…
Kun Ren
  • 4,715
  • 3
  • 35
  • 50
6
votes
3 answers

Using separate from tidyr with different length vectors

I would like to separate a column of strings such as [1, 58, 10] into columns using separate from tidyr. My problem is that sometimes the columns are shorter (never longer). I have many columns with this issue in the same data frame. Loading…
bytesinflight
  • 1,624
  • 2
  • 17
  • 28
6
votes
4 answers

Extract part of string between two different patterns

I try to use stringr package to extract part of a string, which is between two particular patterns. For example, I have: my.string <- "nanaqwertybaba" left.border <- "nana" right.border <- "baba" and by the use of str_extract(string, pattern)…
Marta Karas
  • 4,967
  • 10
  • 47
  • 77
5
votes
1 answer

Why can't I supply str_detect with a column name argument?

I have this toy data as df: structure(list(Product_Name = c("Delicious Chips", "Creamy Tomato Soup", "Cheesy Macaroni", "Savory Meatballs", "Crispy Chicken Tenders" ), Ingredients = c("Potato Slices | Vegetable Oil | Salt | Seasoning Blend",…
Jay Bee
  • 362
  • 1
  • 9
5
votes
2 answers

R Regex for Postive Look-Around to Match Following

I have a dataframe in R. I want to match with and keep the row if "woman" is the first or the second word in a sentence, or if it is the third word in a sentence and preceded by the words "no," "not," or "never." phrases_with_woman <-…
generic
  • 302
  • 1
  • 3
  • 14
5
votes
3 answers

Remove all characters between two other characters (keeping the other characters)

I know this question has been asked multiple times in different ways, but I can't find a solution where I would replace the text between some "borders" while keeping the borders. input <- "this is my 'example'" change <- "test" And now I want to…
deschen
  • 10,012
  • 3
  • 27
  • 50
5
votes
3 answers

How can I add NA values conditionally in a vector in R?

Let's say my data is df <- c("Author1","Reference1","Abstract1","Author2","Reference2","Abstract2","Author3","Reference3","Author4","Reference4","Abstract4"). This is a series in which the order is Author, Reference and Abstract. But in some cases,…
Eva
  • 663
  • 5
  • 13
5
votes
4 answers

In dplyr using str_detect and case_when in R

This is my df: mydf <- structure(list(Action = c("Passes accurate", "Passes accurate", "Passes accurate", "Passes accurate", "Lost balls", "Lost balls (in opp. half)", "Passes (inaccurate)", "Interceptions (in opp. half)", "Interceptions",…
Laura
  • 675
  • 10
  • 32
5
votes
5 answers

Split columns considering only the first dot in R using separate

This is my dataframe: df <- tibble(col1 = c("1. word","2. word","3. word","4. word","5. N. word","6. word","7. word","8. word")) I need to split in two columns using separate function and rename them as Numbers and other called Words. Ive doing…
Laura
  • 675
  • 10
  • 32
5
votes
3 answers

Transforming complete age from character to numeric in R

I have a dataset with people's complete age as strings (e.g., "10 years 8 months 23 days) in R, and I need to transform it into a numeric variable that makes sense. I'm thinking about converting it to how many days of age the person has (which is…
Ruam Pimentel
  • 1,288
  • 4
  • 16
5
votes
2 answers

Put multiple functions into a single dplyr mutate across everything e.g. change mulitple different strings

Example data: df1 = data.frame(x1 = rep(c("foo", "bar"), 4), x2 = rep(c("FOO", "fix", "broke", "fix"), 2)) I want to, for example, change multiple different strings, in this case change foo to done and bar to open. I am using…
Mark Davies
  • 787
  • 5
  • 18
5
votes
3 answers

`stringr` to convert first letter only to uppercase in dataframe

I would like to capitalize the first letter of each word in a column, without converting remaining letters to lowercase. I am trying to use stringr since its vectorized and plays well with dataframes, but would also use another solution. Below is a…
maia-sh
  • 537
  • 4
  • 14
5
votes
2 answers

Extracting a string from one column into another in R

I have an example data frame like the one below. ID File 1 11_213.csv 2 13_256.csv 3 11_223.csv 4 12_389.csv 5 14_456.csv 6 12_345.csv And I want to add another column based on the string between the underscore and the period…
beanboy
  • 217
  • 1
  • 9