This question already has answers here:
Extracting the last n characters from a string in R
(15 answers)
Closed 3 years ago.
I have an element in my dataframe which I want to modify.
I have a column with the following type of values
https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345
I want to replace the entire string with just the last 5 characters (i.e)
Replace the entire character string with 12345 in this case.
How do I achieve this?
Thanks a lot.
One option is using a positive look behind using stringr::str_extract
str_extrct('https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345',
'(?<=proposalId\\=)\\d+')
#Simple option
str_extract('https://mns-xyz-eu.abc.com/ccs/proposal?action=view&proposalId=12345', '\\d+')
Related
This question already has answers here:
Extracting a string between other two strings in R
(4 answers)
Closed 1 year ago.
I have an object created from an rtf document using the code:sample_doc <- read_rtf("sample.doc") (I had to use read_rtf because the document is actually an rtf).
I know somewhere in the document there are two phrases (an element in the character vector) apple and orange and that there must be an element in between them. I just want to extract that in-between element. What should I do?
Thanks!
You can use positive lookbehind and lookahead to target the pattern in between, this regex should give u what u need:
(?<=orange)(.*)(?=apple)
This question already has answers here:
Replace all particular values in a data frame
(8 answers)
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 4 years ago.
I have a large data frame with a character value in various columns and rows. The character is [subnet].
I am trying to get rid of it by writing the following code
new_data[]=lapply(new_data, gsub, pattern="[subnet]", replacement='')
However, it seems like although "subnet" is disappearing, I am ending up with
"[]" for every instance of the character.
Any idea or fix would be appreciated.
This question already has answers here:
How can I remove repeated characters in a string with R?
(3 answers)
Closed 4 years ago.
I have a string: string <- "YYYYYXXXYYXZYYZ" and I want to retain only a single copy of the repeated elements, so that string would read the following: "YXYXZYZ". What is the best way to do this R?
This with gsub:
gsub('([[:alpha:]])\\1+', '\\1', string)
From another answer fom #Yihui Xie at How can I remove repeated characters in a string with R?
This question already has answers here:
How to use the strsplit function with a period
(3 answers)
Closed 5 years ago.
I apologize for possible similar questions, but I just can't find the solution for my problem. So, I have a string with three parts, separated by “.”, for example:
a <- "XXX.YY.ZZZ"
(the length of strings differ, it could also be a <- "XXXX.Y.ZZ", but the three parts are always separated by the two “.”.
I solved the problem for the first part:
library(stringi)
stri_extract(a, regex='[^.]*')
[1] "XXX"
Appreciate your help.
hello you can use strsplit as follows
strsplit(a,"\\.")[[1]]
This question already has answers here:
Double quotes not escaped in R
(1 answer)
Get indices of all character elements matches in string in R
(1 answer)
Closed 5 years ago.
I want to do two thing:
1) I want to create a character string with a double quote inside. An example in R would look like follows:
x <- 'vjghvbh"kljnj"kjbn"jk'
[1] "vjghvbh\"kljnj\"kjbn\"jk"
Question 1: How could I create such a character string without the backslash inside?
I tried to use gsub(), but unfortunately that didn't work. I also found some sources, which suggested cat(), but that just prints my character, but does not store it in x.
2) Let's assume that I solved Question 1. Then my character would look like follows:
[1] "vjghvbh"kljnj"kjbn"jk"
Now I need to find the positions of the double quotes. Based on this thread I tried gregexpr(). However, this also did not work, since I was not able to specify the pattern.
Question 2: How could I find the position of the double quotes within my character string?
The result in R should look like this:
[1] 8 14 19