-4

I don't understand the difference between "=" and "==" in R. I have a few questions:

  • Why does = assign when in a script but not when in a function?
  • Why should I use <- when = exists? Is there a difference?
sebastian-c
  • 15,057
  • 3
  • 47
  • 93
miwst
  • 29
  • 3
  • 3
    I'm really not certain why questions like this get downvoted **so heavily** *given that* some of the most highly voted questions in [r] on SO are things like: http://stackoverflow.com/q/7014387/1465387. I get the RTFM aspect, but this question is fairly hard to google and even look up in the documentation without a little bit of experience. – sebastian-c Jan 21 '13 at 07:53
  • @sebastian-c, you don't have to read a lot of the basic R-documentation to get to explanations of the differences between `=` and `==`. Furthermore, OP's question have nothing about what have been tried before posting on SO. Was `?'='` and `?'=='` tried? What in the online documentation was unclear. I think this is the kind of stuff that makes people down vote questions like this. – Eric Fail Jan 21 '13 at 08:06
  • @DWin I'm aware of that, but I think that someone new to R could easily quote that. A quick search of SO shows that this hasn't been asked before. Questions like this have often been the source of some of the most interesting answers about how R works. I'm not disputing that the answer is easy to find, I just think that it's not **-5** bad. – sebastian-c Jan 21 '13 at 08:13
  • @miwst, [How to Ask](http://stackoverflow.com/questions/how-to-ask) at SO. The first bullet is _do your homework_. In my opinion this is crucial, show that you have tried on your own before you start taking op other peoples time. I also want to recommend [Jon Skeet's blog post _Writing the perfect question_](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx). It's very informative and will help you help us in helping you and others. – Eric Fail Jan 21 '13 at 08:14
  • The additional questions added by the edit are a duplicate of [this FAQ](http://stackoverflow.com/q/1741820/1412059). – Roland Jan 21 '13 at 08:25
  • I am glad that sebastian-c can understand. I did search online, and I searched for similar questions on stackoverflow too. I am sorry to ask dumb question - I am very beginner at both programming and R. But now I understand, so thank you everyone. I searched ?= and ?== on R, but I didn't know I need to search for ?'=' and ?'=='. Also I found that the explanations I find on R are very hard to get my head around :( – miwst Jan 21 '13 at 09:07

2 Answers2

9

I've never written a line of r but I can tell you with almost total certainty that = is the assignment operator, while == is the equality operator. A quick google search will easily answer questions you have like this.

It seems that the arrow operator <- is more typically used in R for assignment, than =.

Assignment:

x = 3
x <- 3

Test for equality:

if (x == 3)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

= is used for assignment and setting function parameters.

== is used for comparing variables: testing for equality.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
Sumit Rai
  • 65
  • 5
  • 13