This question already has answers here:
Creating a comma separated vector
(6 answers)
Closed 6 years ago.
I am trying to create a comma separated string exactly like this to put in a url: 1,2,3,4,5 but when I try: paste0(1:5,sep=",") it returns: [1] "1," "2," "3," "4," "5," The spaces are causing issues. How do I get rid of them?
This is not the same question as: this SO post the solution is almost the same, but the question approaches it from a different angle. My question is trying to suppress the spaces, the linked question wants them.
Try to use collapse="," instead of sep=",":
paste0(1:5,collapse=",")
[1] "1,2,3,4,5"
Related
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+')
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 an answer here:
grepl not searching correctly in R
(1 answer)
Closed 6 years ago.
I want to know if there is a certain string contained in another string. This works fine here:
grepl("a","a")
However, what I actually want to test is the following and this one doesn't work:
grepl("is.na(x)","is.na(x)")
Can anyone help?
You can escape the special characters like this:
grepl("is\\.na\\(x\\)","is.na(x)")
[1] TRUE