It seems output is same when I use any of the two. Is there any difference between them?
x <- "hello"
x <- 'hello'
x = "hello"
x = 'hello'
It seems all are giving same output. Is there difference between them? and when to use them?
Thanks in advance!
Answer
In your examples, the answer is yes. But see notes below:
https://stat.ethz.ch/R-manual/R-devel/library/base/html/Quotes.html
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.
http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html
A little history before we continue: when the R language (and S before
it) was first created, <- was the only choice of assignment operator.
This is a hangover from the language APL, where the arrow notation was
used to distinguish assignment (assign the value 3 to x) from equality
(is x equal to 3?). (Professor Ripley reminds me that on APL keyboards
there was an actual key on the keyboard with the arrow symbol on it,
so the arrow was a single keystroke back then. The same was true of
the AT&T terminals first used for the predecessors of S as described
in the Blue Book.) However many modern languages (such as C, for
example) use = for assignment, so beginners using R often found the
arrow notation cumbersome, and were prone to use = by mistake. But R
uses = for yet another purpose: associating function arguments with
values (as in pnorm(1, sd=2), to set the standard deviation to 2). To
make things easier for new users familiar with languages like C, R
added the capability in 2001 to also allow = be used as an assignment
operator, on the basis that the intent (assignment or association) is
usually clear by context. So, x = 3
clearly means "assign 3 to x", whereas
f(x = 3)
clearly means "call function f, setting the argument x to 3".
No comments:
Post a Comment