Creating a string in R with " in it [duplicate] - r

This question already has an answer here:
paste quotation marks into character string, within a loop
(1 answer)
Closed 5 years ago.
I am trying to get text given between “ ” to make a string. But because string has two “ already in it, I am not able to do so.
?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA"
When I am trying to input , it is giving me an error.
Input <- "?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA""
I tried many escape characters, but always I got an error message.
Error: unexpected symbol in "input <- "?jql=filter%20=%20"Plan"
any help will be highly appreciated

In the string, there is already a double quote. So, we can wrap it with single quotes
Input <- '?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA'
cat(Input, "\n")
#?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA

"Escape" the character like this
a <- "\""

Related

Why it is unable to write \delta inside a print command in "R"? [duplicate]

This question already has answers here:
Error: '\R' is an unrecognized escape in character string starting "C:\R"
(5 answers)
R - Unable to replace unrecognized escape symbol "\&" in character string
(1 answer)
Closed 24 days ago.
I want to insert a delta inside a print command in R. Expect \delta all the greek letters are coming without any error. However, the following error is prompted when trying to print \delta using the sprintf command. Can you help?
I tried searching for an answer and found one. But the solution was to use a forward slash which obviously won't work in my case.

Replace latex with r strings using gsub [duplicate]

This question already has an answer here:
"'\w' is an unrecognized escape" in grep
(1 answer)
Closed 1 year ago.
I would like to find and replace tabular instances by tabularx. I tried with gsub but it seems to enter me into a world of escaping pain. Following other questions and answers I find fixed=TRUE which is the best I so far have. The code snippet below almost works, \B is unrecognized. If I escape it twice I get \BEGIN as output!
texText <- '\begin{tabular}{rl}\begin{tabular}{rll}'
texText <- gsub("\begin{tabular}{rl}", "\BEGIN{tabular}{rll}", texText, fixed=TRUE)
I'm using BEGIN as my test to see what is happening. This is before I get to tackling the question of what goes on in the brackets {rl} {ll} {rrl} etc. Ideally I'm looking for a regex that would output:
\begin{tabularx}{rX}\begin{tabularx}{rlX}
That is the final column is replaced by X.
Try using proper escaping:
texText <- "\begin{tabular}{rl}\begin{tabular}{rll}"
output <- gsub("\begin\\{tabular\\}", "\begin{tabularx}", texText)
output
[1] "\begin{tabularx}{rl}\begin{tabularx}{rll}"
A literal backslash requires two backslashes, and also metacharacters such as { and } require two backslashes.

How to remove '+ off' from the end of string? [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 4 years ago.
Similar to R - delete last two characters in string if they match criteria except I'm trying to get rid of the special character '+' as well. I also attached a picture of my output.
When I attempt to use the escape command of '+', I get an error message saying
Error: '\+' is an unrecognized escape in character string starting ""\\s\+"
As you noticed, + is a metacharacter in regex so it needs to be escaped. \+ escapes that character, but \, itself, is a special character in R character strings so it, too, needs to be escaped. This is an R requirement, not a regex requirement.
This means that, instead of '\+', you need to write '\\+'.

Separating "-" from Text in R [duplicate]

This question already has answers here:
R - remove anything after comma from column
(5 answers)
Closed 4 years ago.
I am trying to remove all text after "-" including the "-" such as "Albany-Schenectady, Allentown-Bethlehem" etc.
I tried using Gsub, but having trouble getting the code to work.
#What I tried to make work
gsub("(.*)-.*", "\\1",
That's an incomplete line of code and not really even on the right track for what you've described. This should work.
gsub("-.*", "", vector)
The first argument tells it to grab the hyphen and everything after it to be replaced by the second argument, an empty string. The third argument is the vector you're performing the operation on.

How to put \' in my string using paste0 function [duplicate]

This question already has answers here:
How to escape backslashes in R string
(3 answers)
Closed 5 years ago.
I have an array:
t <- c("IMCR01","IMFA02","IMFA03")
I want to make it look like this:
"\'IMCR01\'","\'IMFA02\'","\'IMFA03\'"
I tried different ways like:
paste0("\'",t,"\'")
paste0("\\'",t,"\\'")
paste0("\\\\'",t,"\\\\'")
But none of them is correct. Any other functions are OK as well.
Actually your second attempt is correct:
paste0("\\'",t,"\\'")
If you want to tell paste to use a literal backslash, you need to escape it once (but not twice, as you would need within a regex pattern). This would output the following to the console in R:
[1] "\\'IMCR01\\'" "\\'IMFA02\\'" "\\'IMFA03\\'"
The trick here is that the backslash is even being escaped by R in the console output. If you were instead to write t to a text file, you would only see a single backslash as you wanted:
write(t, file = "/path/to/your/file.txt")
But why does R need to escape backslash when writing to its own console? One possibility is that if it were to write a literal \n then this would actually be interpreted by the console as a newline. Hence the need for eacaping is still there.

Resources