Hi all, can we use '=' instead of '<-' operator for assignment in R programs? regards, KM
Yes, but <- is preferred. Note, there are also some differences. You can do the following:> a <- 10 > b = 10 > identical(a,b)[1] TRUE And you can also do> myFun <- function(x, y = 100){+ result <- x*y + result}> myFun(x = 20)[1] 2000 But, you cannot use '<-' to define the arguments of a function> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of km > Sent: Wednesday, November 03, 2010 2:05 PM > To: r-help at r-project.org > Subject: [R] '=' vs '<-' > > Hi all, > > can we use '=' instead of '<-' operator for assignment in R programs? > > regards, > KM > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Wed, Nov 3, 2010 at 6:04 PM, km <srikrishnamohan at gmail.com> wrote:> Hi all, > > can we use '=' instead of '<-' operator for assignment in R programs?Yes, mostly, you can also use 'help' to ask such questions: > help("=") The operators ?<-? and ?=? assign into the environment in which they are evaluated. The operator ?<-? can be used anywhere, whereas the operator ?=? is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions. and so on...
@all: Does it seem reasonable to add a discussion of '=' vs. '<-' to the FAQ? It seems a regular question and something of a "hot" topic to debate. @KM Here are links I've accumulated to prior discussions on this topic. I am pretty certain they are all unique. http://blog.revolutionanalytics.com/2008/12/use-equals-or-arrow-for-assignment.html http://www.mail-archive.com/r-help at r-project.org/msg69310.html http://www.mail-archive.com/r-help at r-project.org/msg99789.html http://www.mail-archive.com/r-help at r-project.org/msg104102.html http://www.mail-archive.com/r-help at r-project.org/msg16881.html https://stat.ethz.ch/pipermail/r-sig-teaching/2010q4/000312.html http://r.789695.n4.nabble.com/advice-opinion-on-vs-in-teaching-R-td1014502.html#a1014502 On Wed, Nov 3, 2010 at 11:04 AM, km <srikrishnamohan at gmail.com> wrote:> Hi all, > > can we use '=' instead of '<-' operator for assignment in R programs? > > regards, > KM > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/