# Use of '=' in place of '<-' for assignment in R

#1

Hello,

i was learning about data.table package in R and I came across a line of code

tt=system.time(ans1<-DF[DF\$x==“R” & DF\$y==“h”,])

I used the ‘=’ operator instead of ‘<-’ after ans1 as written below

tt=system.time(ans1=DF[DF\$x==“R” & DF\$y==“h”,])

and i got an error saying

Error in system.time(ans1 = DF[DF\$x == “R” & DF\$y == “h”, ]) :
unused argument (ans1 = DF[DF\$x == “R” & DF\$y == “h”, ])

So I just wanted to know when can we use the ‘=’ operator in place of ‘<-’ and when not.

Thanks.

#2

The <- operator should mostly be used inside the expression,and when doing operations outside of expressions the = can be used.
Consider the following :-

``````> tt=data.frame(ans1 <- DF[DF\$x=="A" & DF\$y=="B",])
> tt
x y
1 A B
> ans1 <- DF[DF\$x=="A" & DF\$y=="B",]
> ans1
x y
1 A B
``````

Now we use the = operator
Outside of expr

``````  > ans1 = DF[DF\$x=="A" & DF\$y=="B",]
> ans1
x y
1 A B
``````

Inside

``````   > tt=data.frame(ans1 = DF[DF\$x=="A" & DF\$y=="B",])
> tt
ans1.x ans1.y
1      A      B
``````

Notice the way the column names have changed.
However,the = operator throws error with the system.time() as you have mentioned.
Hope this helps!!