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

str_count with overlapping substrings

I am trying to count the number of appearances of a substring within a character vector. For example: lookin<-c("babababa", "bellow", "ra;baba") searchfor<-"aba" str_count(lookin, searchfor) returns: 2 0 1 However, I want it to return '3 0 1' but…
garson
  • 1,505
  • 3
  • 22
  • 56
5
votes
2 answers

Regular expression matching inside dplyr

When answering this question, I wrote the following code: df <- data.frame(Call_Num = c("HV5822.H4 C47 Circulating Collection, 3rd Floor", "QE511.4 .G53 1982 Circulating Collection, 3rd Floor", "TL515 .M63 Circulating Collection, 3rd Floor", "D753…
Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
5
votes
1 answer

Perl regular expressions in the stringr package

The perl() function is deprecated in the latest version of stringr in favor of regex(). However, I don't seem to be able to replicate the earlier behavior. To capitalize the first letter of a vector of strings, this used to work: name <- c("jim",…
user2987808
  • 1,387
  • 1
  • 12
  • 28
5
votes
2 answers

dplyr: use chaining to pass variables

I'm new to dplyr and cannot figure out how to control the variables to pass through a chaining (%>%) command. Simple example: the str_sub function takes three arguments - the first is passed on through %>% but how can I get the last two?…
user3375672
  • 3,728
  • 9
  • 41
  • 70
5
votes
5 answers

R string containing only one type of character

I would like to check if a string contain only one type of character For example INPUT: str = "AAAAB" char = "A" OUTPUT: str contains only char = FALSE With grepl(char,str) the result is TRUE but I want it to be FALSE. Many thanks
user3910073
  • 511
  • 1
  • 6
  • 23
5
votes
1 answer

remove quotation marks from string at beginning and end only if both are present

I want to clean up a string that contains escaped quotation marks. I want to remove the escaped quotation marks the end and beginning of a string but keep intact all qoutation marks within the string. What I came up with is the…
Mark Heckmann
  • 10,943
  • 4
  • 56
  • 88
4
votes
1 answer

Why does R `stringr::str_extract('word. 42', pattern = '\\d*')` not produce `"42"`?

I have a vector of strings of the form "letters numbers", I want to extract the numbers using RegEx implemented in stringr::str_extract with pattern "\\d*". The results are very confusing: # R 4.2.3 # install.packages('stringr') library(stringr) #…
George Pak
  • 43
  • 4
4
votes
3 answers

Extract n characters after pattern in string in R

I have a long string with mupltiple instances of pattern. I want the n characters following the pattern. Say that my string is "quick fox jumps over the lazy dog" and I want the two characters after every "u". i.e. I would want a vector c("ic",…
Tordir
  • 191
  • 6
4
votes
4 answers

Problem pasting 3 strings with different separator

Probably it´s a nonsense but I´ve been playing around with paste and paste0 with no success. My data frame looks like this stck <-structure(list(haul = 1:11, year = c(1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,…
Juan Carlos
  • 173
  • 13
4
votes
3 answers

Split a string and keep delimiter

Lets say I have a string: StringA/StringB/StringC Is there any way I can split this string by the / symbol, but keep it in the returned values: StringA /StringB /StringC
jackahall
  • 400
  • 1
  • 7
4
votes
2 answers

extract string from multiple columns in new column

I want to find a word in different columns and mutate it in a new column. "data" is an example and "goal" is what I want. I tried a lot but I didn't get is work. library(dplyr) library(stringr) data <- tibble( component1 = c(NA, NA, "Word",…
4
votes
3 answers

R Extract first two characters from a column in a dataframe

I have a dataset with multiple and I would like to extract the first two characters from the sr column. Lastly, these characters will be stored in a new column. Basically, I want to have a new column permit_type that has the first two character…
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
4
votes
1 answer

Using string matching like grepl in a dbplyr pipeline

dbplyr is very handy as it convert dplyr code into SQL. This works really well except when it doesn't. For example i am trying to subset rows by partially matching a string against values in a column. With exception of postgres, it appears as though…
boshek
  • 4,100
  • 1
  • 31
  • 55
4
votes
3 answers

Show adjacent members in a list

I want to inspect adjacent elements in a list based on a match. For example, in a list of randomly ordered letters, I want to know what the neighboring letters of m is. My current solution is: library(stringr) ltrs <-…
msn
  • 113
  • 3
4
votes
3 answers

How to remove Unicode representations of Emojis in strings using regexp in R?

I am working with data from the Twitter API and wherever users had included Emojis in their name field, they have been translated to Unicode string representations in my dataframe. The structure of my data is somewhat like this: user_profiles <-…
whatevr
  • 43
  • 4