To declare a character or a string on R, one can use both following ways:
x <- 'Some string'
x <- "Some string"
Both work, but is there any difference ?
From ?"'":
Details
Three types of quotes are part of the syntax of R: single and double
quotation marks and the backtick (or back quote, `). In addition,
backslash is used to escape the following character inside character
constants.
Character constants
Single and double quotes delimit character constants. They can be used
interchangeably but double quotes are preferred (and character
constants are printed using double quotes), so single quotes are
normally only used to delimit character constants containing double
quotes.
Backslash is used to start an escape sequence inside character
constants. Escaping a character not in the following table is an
error.
Single quotes need to be escaped by backslash in single-quoted
strings, and double quotes in double-quoted strings.
No. These are identical.
......
Related
I want to remove "Portugal" remove double quotes and replace it with string empty what to do i am using Replace("""," ") but this not working
Use backslash n instead:
Replace("\"", string.Empty);
The backslash ("\") character is a special escape character used to indicate other special characters such as quotation marks (\"). For other special escape characters, you can refer to this link:
https://msdn.microsoft.com/en-us/library/aa691087(v=vs.71).aspx
suppose I have a string that has the following characters
"\"------------080209060700030309080805\""
And now I want to use gsub function in r to remove the "\ and \" part, and only keep the following characters:
"------------080209060700030309080805\"
Could anyone help me to figure out how should I do it properly ?
Edit 1: Fixed bug (two backslashes required to create a backslash in a string):
s <- '\\"------------080209060700030309080805\\"'
s
gsub('\\"', "", s, fixed = TRUE)
results in
> s <- '\\"------------080209060700030309080805\\"'
> s
[1] "\\\"------------080209060700030309080805\\\""
> gsub('\\"', "", s, fixed = TRUE)
[1] "------------080209060700030309080805"
Please note that a single backslash in R is the escape code which is NOT part of the string:
> charToRaw('\\"')
[1] 5c 22
> charToRaw('\"')
[1] 22
Therefor you have to use two backslashes in the quoted string to create one backslash internally. If you print this string the backslash is escaped again which looks confusing:
> print('\\"')
[1] "\\\""
If you want to print the unescaped content of the string use cat instead of print:
> cat('\\"')
\"
For more see help in R: ?"'":
Character constants
Single and double quotes delimit character constants. They can be used
interchangeably but double quotes are preferred (and character
constants are printed using double quotes), so single quotes are
normally only used to delimit character constants containing double
quotes.
Backslash is used to start an escape sequence inside character
constants. Escaping a character not in the following table is an
error.
Single quotes need to be escaped by backslash in single-quoted
strings, and double quotes in double-quoted strings.
\n newline \r carriage return \t tab \b backspace \a alert (bell)
\f form feed \v vertical tab \ backslash \ \' ASCII apostrophe '
\" ASCII quotation mark " ` ASCII grave accent (backtick) ` \nnn
character with given octal code (1, 2 or 3 digits) \xnn character
with given hex code (1 or 2 hex digits) \unnnn Unicode character with
given code (1--4 hex digits) \Unnnnnnnn Unicode character with given
code (1--8 hex digits)
string <- "\\------------080209060700030309080805\\"
string <- gsub("^\\\\(.*)\\\\$", "\\1", string)
Notes: The pattern I used was ^\(.*)\$, which will match everything in between a beginning and ending backslash. This would only match strings therefore which both begin and end with backslash. Also, we use four backslashes (\\\\) to represent a literal backslash for the pattern in gsub(). We need to escape twice, once for R, and a second time for the regex engine.
I am using rewrite module to allow double quotes in string
[_0-9a-z-‘!##$%^*()!~`\22\42.]
But it gives the following error.
The expression "^([_0-9a-z-]+)/([_0-9a-z-]+)/([_0-9a-z-‘!##$%^*()!~`\22\42.]+)" contains an escape sequence that is not valid.
The \22 and \42 are not valid escape sequences. You need to specify the hex notation if you plan to match those characters:
[_0-9a-z-‘!##$%^*()!~`\u0022\u0042.]
Or
[_0-9a-z-‘!##$%^*()!~`\x22\x42.]
This is perhaps rather a minor question...
but just a moment ago I was looking through some code I had written and noticed that I tend to just use ="something" and ='something_else' completely interchangeably, often in the same function.
So my question is: Is there R code in which using one or other (single or double quotes) has different behaviour? Or are they totally synonymous?
According to http://stat.ethz.ch/R-manual/R-patched/library/base/html/Quotes.html, "[s]ingle and double quotes delimit character constants. They can be used interchangeably but double quotes are preferred (and character constants are printed using double quotes), so single quotes are normally only used to delimit character constants containing double quotes."
Just for curiosity, there is a further explaination in R-help mailing list for Why double quote is preferred in R:
To avoid confusion for those who are accustomed to programming in the
C family of languages (C, C++, Java), where there is a difference in
the meaning of single quotes and double quotes.
A C programmer reads 'a' as a single character and "a" as a character
string consisting of the letter 'a' followed by a null character to
terminate the string.
In R there is no character data type, there are
only character strings. For consistency with other languages it helps
if character strings are delimited by double quotes. The single quote
version in R is for convenience.
(Since) On most keyboards you don't need to
use the shift key to type a single quote but you do need the shift for
a double quote.
> print(""hi"")
Error: unexpected symbol in "print(""hi"
> print("'hi'")
[1] "'hi'"
> print("hi")
[1] "hi"
Windows copies path with backslash \, which R does not accept. So, I wanted to write a function which would convert \ to /. For example:
chartr0 <- function(foo) chartr('\','\\/',foo)
Then use chartr0 as...
source(chartr0('E:\RStuff\test.r'))
But chartr0 is not working. I guess, I am unable to escape /. I guess escaping / may be important in many other occasions.
Also, is it possible to avoid the use chartr0 every time, but convert all path automatically by creating an environment in R which calls chartr0 or use some kind of temporary use like using options
From R 4.0.0 you can use r"(...)" to write a path as raw string constant, which avoids the need for escaping:
r"(E:\RStuff\test.r)"
# [1] "E:\\RStuff\\test.r"
There is a new syntax for specifying raw character constants similar to the one used in C++: r"(...)" with ... any character sequence not containing the sequence )". This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see ?Quotes.
Your fundamental problem is that R will signal an error condition as soon as it sees a single back-slash before any character other than a few lower-case letters, backslashes themselves, quotes or some conventions for entering octal, hex or Unicode sequences. That is because the interpreter sees the back-slash as a message to "escape" the usual translation of characters and do something else. If you want a single back-slash in your character element you need to type 2 backslashes. That will create one backslash:
nchar("\\")
#[1] 1
The "Character vectors" section of _Intro_to_R_ says:
"Character strings are entered using either matching double (") or single (') quotes, but are printed using double quotes (or sometimes without quotes). They use C-style escape sequences, using \ as the escape character, so \ is entered and printed as \, and inside double quotes " is entered as \". Other useful escape sequences are \n, newline, \t, tab and \b, backspace—see ?Quotes for a full list."
?Quotes
chartr0 <- function(foo) chartr('\\','/',foo)
chartr0('E:\\RStuff\\test.r')
You cannot write E:\Rxxxx, because R believes R is escaped.
The problem is that every single forward slash and backslash in your code is escaped incorrectly, resulting in either an invalid string or the wrong string being used. You need to read up on which characters need to be escaped and how. Take a look at the list of escape sequences in the link below. Anything not listed there (such as the forward slash) is treated literally and does not require any escaping.
http://cran.r-project.org/doc/manuals/R-lang.html#Literal-constants