Saturday 6 April 2019

Different behavior in using '=' versus '










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

Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...