2,594
questions
1
vote
1
answer
37
views
Pull items from a named list based on string pattern R
I'm trying to pull similarly named items out of a list based on a shared string pattern for instance
> df1<- data.frame(1:5)
> df2 <-data.frame(11:15)
> df3 <-data.frame(26:30)
> ...
0
votes
1
answer
44
views
How to collapse vectors inside a dataframe into strings
I'm creating a new variable inside my dataframe from str_extract_all, which results in a column with some vectors inside it. A similar result comes from this manipulation of iris:
t = iris %>% ...
0
votes
0
answers
47
views
Custom Function Using Stringr Package and Curly Brackets
I am performing a systematic review and have a mix of studies that contain both mean (SD) and median (IQR). I'm trying to create a function to convert median (IQR) to mean (SD) using the Wan method (...
0
votes
0
answers
56
views
Is there a way to speed up str_extract in my function
I am currently creating an R package with the following function:
# Define the function to process each chunk
process_chunk <- function(chunk, ictv_formatted, taxa_rank) {
taxon_filter <- ...
3
votes
3
answers
78
views
Using str_replace_all or gsub (regex) to replace each occurrence of substring with unique substring - R
I have a column in a dataframe with street addresses like this:
df_col <- c("100 W 10th St", "200 Drury Ln 2a", "300 W 10th St", "400b Drury Ln")
I want to ...
1
vote
2
answers
48
views
In R, how do you find the number of different values between 2 character strings?
I'm trying to see the number of new employees a manager got between time one and time 2. I have a string of all employee ids that roll up under that manager.
My below code always says there is 1 new ...
2
votes
3
answers
89
views
How to remove all occurrences of an underscore before a pattern?
In R, how do we remove all occurrences of an underscore before a pattern?
For example:
x <- c("a_b_test_1", "a_b_c_test_2")
I would like to achieve:
x
[1] "ab_test_1" ...
0
votes
2
answers
108
views
Replacing string values with dplyr
I have a dataframe in R:
df <- data.frame(CRP = c("10", "2", "3", NA, "<4", ">5"))
I want to create a numeric column that replaces the ...
1
vote
2
answers
48
views
Get strings before special character except apostrophe using stringr::word
Character string:
text="Il n’a pas réussi à répondre à votre plainte/question et ne vous a pas orienté ailleurs"
I'd like to extract the part before "/". I tried
word(text,sep=&...
3
votes
5
answers
90
views
Order columns based on suffix with dplyr and stringr
I have a dataframe that has columns that are separated out by suffix see ex_df. Each time I run the code there may be varying column amounts based on database entries so I am trying to find a way to ...
1
vote
2
answers
46
views
Remove string from column across group of rows in another column
I would like to remove a string from one column across a group of rows in another column. In the below reprex, I would like to remove the string in snippet from the string in text in any row in the ...
3
votes
2
answers
111
views
Using R, how can I detect if a string includes a unicode character?
If a string includes a Unicode character, I would like to detect it and replace it (because RMarkdown fails when creating pdf output that includes Unicode characters). For example, the variable ...
1
vote
0
answers
48
views
Convert ordinal word string to ordinal number string? [duplicate]
Suppose I have a vector containing ordinal number words as strings in R:
ordinal_words<-c("FIRST COMPANY", "BAKERY FIFTH AVENUE", "WEST FORTIETH WAY")
I would like ...
6
votes
7
answers
396
views
Count empty strings?
In R, suppose I have a vector like:
vector<-c("Red", " ", "", "5", "")
I want to count how many elements of this vector are just empty strings ...
1
vote
1
answer
25
views
Changing isolated words in R?
In R, I have a character vector like:
vector<-c("BAKING CO", "NEW COBALT", "CO INC")
I would like to convert the word "CO" to "COMPANY", but only ...
1
vote
2
answers
80
views
Replace ordinal numbers in R?
In R, suppose I have a string vector composed of words:
string1<-c("1ST COMPANY", "21ST LIBRARY")
However, within these strings, I want to convert ordinal number words like &...
4
votes
3
answers
86
views
How to extract birth and death year from string in R?
I have the first paragraph of Wikipedia articles from the wikifacts package (only for people). I like to extract birth year and year of death.
library(wikifacts)
library(tidyverse)
politicians <- ...
3
votes
2
answers
86
views
Combining singleton letters?
I have the following character vector:
vec1 <- c("D R JOHNSON", "NICE W E A T H E R")
This vector has consecutive series of single letters like "D R" which I do NOT ...
0
votes
2
answers
27
views
splitting string into new columns different criteria in R
I have 2 different nomenclatures and want to split the patient ID and timepoint
Original dataframe
patient_id_and_timepoint
12345 V1
101-123-V1
Desired dataframe
patient_id
timepoint
12345
V1
...
1
vote
3
answers
63
views
how to search column for multiple strings in R
I have a list of characters and I would like to search for them and a create a column for them in R.
antifungals <- c("caspofungin", "fluconazole")
Here is an example:
Original ...
0
votes
1
answer
29
views
How to insert characters after a pattern using stringr?
I have a character vector
xx <- c ("AB", "BA", "CA")
I would like to insert the word "hello" after the first element, but only if this element is not A. I ...
1
vote
2
answers
44
views
What is the correct syntax for specifying length for positive look behind?
I have a series of expressions that start with numbers of different length and I would like to extract the string after these numbers: e.g. c("14ac", "148me3") I would like to get ...
1
vote
4
answers
87
views
What is the most elegent way to insert characters (passed on as a vector) at a certain position in the string (passed on as vector) using stringr?
I need to insert the following characters: c("me3", "ac") in positions c(1, 6) in the following string KSTGGKAPR. What is the most elegant way to do this? Can I avoid writing a ...
2
votes
1
answer
65
views
Extract *all* possible patterns in a variable
I have a large variable containing strings (words). I need to extract all substrings that contain any of the patters listed in a separate vector.
library(tidyverse)
df <- data.frame(Word = c("...
2
votes
1
answer
44
views
Why does str_sub (from stringr) report an error when the string is passed to it explicitly, while it works if the string is passed as a variable?
On my computer:
str_sub("TKQTAR", 2, 2) <- "Kme1"; x
returns
Error in str_sub("TKQTAR", 2, 2) <- "Kme1" :
target of assignment expands to non-language ...
0
votes
1
answer
32
views
Splitting a numeric string column into a series of indicator columns
I am working on a data analysis project where I was given a very messy data set. To contextualize, the data is for mosquito surveillance. One of my columns is a series of numbers seperated by " &...
0
votes
1
answer
63
views
Preserving Factor Order in scale_y_discrete with str_wrap in ggplot2 for Likert Data
I am trying to create a heat plot for Likert scale data using the likert package in R, and I want to wrap the y-axis labels using str_wrap to make them more readable. However, when I use str_wrap with ...
1
vote
1
answer
74
views
Extracting all characters from the beginning of a string through the first 5 digits of last portion of string?
I am working in R with some really messy address data and have been able to solve every issue except one. At the end of the address string, where the zip code is -- there is often 1, 2, or 3 ...
-2
votes
1
answer
45
views
string remove pattern with special character in R
I have an object in R called text :
text = c(" - John - Loves Helen - Day 4")
I want to remove the starting space the "-" and the space (i.e) all the special characters before ...
1
vote
2
answers
91
views
How to use regular expression to match two words that have alphabetically-adjacent first letter?
I recently learnt that () can be used for define patterns in regular expression, and use \\1 can refer to the first set of string defined in the ().
It is a powerful idea because I can use it to ...
2
votes
1
answer
50
views
How to replace all occurrences of a certain character, but not if the character is the first in string?
I have the following vector
x = c("AXX", "XAX", "XXA")
I would like to replace all the "A" to "B" in x, but not if "A" is at the beginning ...
0
votes
2
answers
26
views
How to perform replacements containing square brackets with string_replace()?
I have a vector with two elements:
xx <- c("K[Phenylisocyanate (N-term)]SAPATGGVKKPHR", "A[Phenylisocyanate (N-term)]SAPATGGVKKPHR")
I would like to replace "[...
2
votes
3
answers
109
views
Extract Street Name from Address in R
I am trying to extract the street names from a street address. I just want to extract the street/house number at the beginning of the string, and extract everything after the last street suffix (RD, ...
0
votes
2
answers
55
views
how to make a data.frame from truncated information and regex in R/Python?
I have a data.frame in R with one column and truncated lines that I would like to divide into three columns via regex, for example:
dados <- c(
"N 2022NE001264 75",
"FRETES ...
4
votes
2
answers
90
views
Base R grep-family is much faster than `stringr` variants when dealing with factors
I have been using stringr since it's supposed to be faster, but I found out today that it's much slower when dealing with factor terms. I didn't see any warning that this would be the case nor why it ...
2
votes
5
answers
105
views
R - mutate using case_when with str_detect and & operator
Respondents to a survey (rows) will give a colour as an answer in one of four columns. The other three columns have the value "none". I am trying to create a new column that picks up the ...
0
votes
3
answers
52
views
Flawed logic with RegEx and numeric ranges
I'm trying to create a new variable called 'group' in a dataset called 'data'. The variable 'group' should take the value "A" or "B" depending on how another variable in the ...
0
votes
2
answers
58
views
Is there a way to extract the part of string in R [duplicate]
For this I want to get the string immediately after D:\. Any thought?
library(stringr)
str_extract("D:\Directory A\File.txt", "Directory A")
I get
Error: '\D' is an unrecognized ...
1
vote
2
answers
44
views
How do I correctly extract my intended values using str_extract_all?
I have the following toy dataframe:
df <- data.frame(
product = c("apple", "banana", "cherry", "durian", "eggplant", "fuyu"),
...
0
votes
2
answers
39
views
How to remove end of string in R having a specific condition, but keep part of condition?
I have a dataset of over 15.000 lines where I want to remove the ending of the string if the condition is a regular expression of the form ([a-z]{1}\\.). How can I remove everything behind the letter, ...
0
votes
3
answers
53
views
Place a character at a certain place in a string, only if the string is a certain number of characters [duplicate]
I have strings that are structured like this:
vector <- c("CBR0", "CBR1", "CBR2", "CBR3", "CBR4", "CBR5", "CBR10", "CBR11&...
16
votes
5
answers
1k
views
How can I subtract a number from string elements in R?
I have a long string. The part is
x <- "Text1 q10_1 text2 q17 text3 q22_5 ..."
How can I subtract 1 from each number after "q" letter to obtain the following?
y <- "...
0
votes
1
answer
24
views
Is there a R code that can detect and separate strings containing characters that are similar?
I have a dataset (with 100k rows) with strings that contain almost similar names, but I need to separate the names.
fertilizer_name<-c('Urea (inorganic)', 'solid manure (organic)')
df1<-data....
0
votes
0
answers
35
views
Create a flexible key for merge in R
I need to perform a merge between two databases but, due to numerous typing errors, I would like to make the criteria for this merge more flexible so that in cases where the keys differ by just one ...
0
votes
1
answer
44
views
Extracting character sequences from text using the stringr package in R
I have a column with texts, named 'OBSERVA.' In the midst of this text, there may be a sequence of 8 digits corresponding to a code that I would like to extract for filling another column. For example,...
0
votes
2
answers
171
views
Difference between using str_detect() and contains()?
I know it might be a silly question, but I was curious if there was any difference, I like more using str_detect because the syntax makes more sense in my brain.
0
votes
0
answers
37
views
Catching a pattern anywhere within a string in R [duplicate]
In My_CODE below, I was wondering if there might be a way to replace endsWith() with another function that captures the pattern anywhere (not just the end of a string)?
I tried tidyselect::contains ...
0
votes
2
answers
45
views
str_replace_all by grouping
I need to replace abbreviated names with long ones.
However, I'm getting the wrong behavior in a case that shouldn't be replaced. This replacement should respect the country condition.
I'm using:
...
4
votes
3
answers
83
views
How to extract multiple numbers between a repeating pattern using stringr?
I have a column of strings that look like the string below, where the numbers following the double colons "::" are ages. In this example, 51, 40, 9, 5, 2, and 15 are the ages. The numbers ...
0
votes
1
answer
29
views
Replace multiple patterns with different replacements in R
I'm probably blind in one eye, but I can't find a "smarter" solution to my problem. I want to replace the emojis with their description which are stored in a separate dataframe. Is there a ...