2

I have read past SO questions about this and their example as to why we need to use <- not = does not seem applicable (at least in the recent versions).

median(x = 1:10)  # Should give an error but both work just fine.
x   

median(x <- 1:10)
x   

I always use =, BUT seem to finally found a case where <- is necessary but don't understand why, can anyone tell me why <- is necessary here and = does not work. Thank you.

x=c(2,4,6)
names(x)=c('a','b','c')
x

# OR we can do this, but it requires a <-

x='names<-'(x, value=c('a','b','d'))
x='names='(x, value=c('a','b','d'))   #Error: could not find function "names="
x
Community
  • 1
  • 1
Benzle
  • 373
  • 3
  • 9
  • 18
  • 5
    Because `names<-` is an actual function. And its name is defined to be `names<-` whereas `names=` does not exist as a function – Rich Scriven Oct 09 '14 at 01:31
  • `=` is used to pass arguments to functions, and `<-` cannot be used for this. Your second expression (`median( x <- 1:10)`) has the side-effect of creating `x` in the enclosing environment, but does not name the argument passed to `median`. – Matthew Lundberg Oct 09 '14 at 01:32
  • If you want to delete your question, I've deleted my answer. – Rich Scriven Oct 09 '14 at 01:56
  • There are cases where you pass an expression to a function, where `<-` is required. Compare: `system.time(x<-3)` and `system.time(x=3)`. – jlhoward Oct 09 '14 at 06:19

0 Answers0