Why doesn't arrow assignment in R work when calling a transform function?

I'm new to R and everything I've read said that the arrow assignment operator is generally preferred a <- 1

over the normal assignment operator a = 1

.

This was fine until I tried to use the transform () function where I noticed that the assignment did not actually happen.

So, if I try the following, sum_x and mean_x are not added to the dataframe. If I tried to update an existing variable in the dataframe instead, it won't update.

my_data <- data.frame(x1 = c(2, 2, 6, 4), x2 = c(3,4,2,8))
transform(my_data, sum_x <- x1 + x2, mean_x <- (x1 + x2)/2)

      

However, using the assignment operator =

works here.

my_data <- data.frame(x1 = c(2, 2, 6, 4), x2 = c(3,4,2,8))
transform(my_data, sum_x = x1 + x2, mean_x = (x1 + x2)/2)

      

I would like to understand why this is the case, I know when I should use each assignment method so as not to get caught up in an unexpected pitfall.

+3


source to share


1 answer


You are told to prefer <- over = because there are cases where the result can be ambiguous. This is however only for cases where you are assigning to a variable. In your example, this is not the case.

The equals = operator is used to assign values ​​to functional parameters.

The transform function uses the syntax = so you can change the environment, but you don't directly assign the results to these variables. transform does this for you and knows how to do it due to the specific syntax you are using.

The trick is to just look at help (? Transform in this case) and follow it.

Adding an example to show why this is important:

mean(x = 1:5)

      



means to find the mean value 1,2,3,4,5. It assigns 1: 5 to the parameter x.

mean(a <- 1:5)

      

works, but doesn't do what you expected. There is no parameter a, so it creates a variable a and assigns it 1: 5. It is then positioned with x.

mean(a = 1:5)

      

doesn't work because there is no parameter called a in the middle function, and the context forces R to do the parameter assignment.

+2


source







All Articles