Different behavior in using '=' versus '<-' operator while assigning a dataframe in R [duplicate]

StackOverflow https://stackoverflow.com//questions/11676179

  •  12-12-2019
  •  | 
  •  

Question

Possible Duplicate:
Assignment operators in R: '=' and '<-'

I would like to know why there is a difference between using = and <- while assigning a dataframe.

Case a: Using =

set.seed(100);a <- data.frame(a1=rnorm(10),a2=sample(c(1,0),10,replace=TRUE))

Case b: Using <-

set.seed(100);b <- data.frame(b1 <- rnorm(10),b2 <- sample(c(1,0),10,replace=TRUE))

Why is there the following difference? Why does not second method retain the variable/column names?

> a
           a1 a2
1 -0.50219235  0
2  0.13153117  0
3 -0.07891709  1
4  0.88678481  1
5  0.11697127  0

>b
  b1....rnorm.5. b2....sample.c.1..0...5..replace...TRUE.
1    -0.50219235                                        0
2     0.13153117                                        0
3    -0.07891709                                        1
4     0.88678481                                        1
5     0.11697127                                        0
Was it helpful?

Solution

Within functions '=' is used as naming or referring the function to a variable of a particular name and <- refers to the assignment function. When R runs it will first resolve '<-" functions within your function parameters. It will then name the variable wither the thing to the left of the equal sign or the full expression in this case "b1 <- rnorm(10)". Finally it will resolve the function (in this case data.frame).

You almost always want to use the '=' within a function. There can be cases where you may want to nest an assignment "<-" but normally this will make your code ridiculous to read.

OTHER TIPS

If you look at ?'data.frame', you'll see the following for the first argument:

"... these arguments are of either the form value or tag = value. Component names are created based on the tag (if present) or the deparsed argument itself."

If you use '<-' instead of '=', data.frame() is reading your input as an expression (assigning rnorm(10) to a1), and not as a value (rnorm(10)) being assigned to a tag (a1)

Just to add to the previous (very good) answers by @Paul and @Edward, here's the consequence of your use of <- instead of = inside data.frame(). Namely, you've created two new objects:

> b1
Error: object 'b1' not found
> b2
Error: object 'b2' not found
> set.seed(100);b <- data.frame(b1 <- rnorm(10),b2 <- sample(c(1,0),10,replace=TRUE))
> 
> b1
 [1] -0.50219235  0.13153117 -0.07891709  0.88678481  0.11697127  0.31863009 -0.58179068  0.71453271 -0.82525943 -0.35986213
> b2
 [1] 0 0 0 0 1 1 0 0 0 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top