为什么R在R中的赋值不能在转换函数调用中工作?

时间:2022-05-18 14:58:06

I'm new to R and everything I have read has said it is generally preferred to the arrow assignment operator a <- 1 over the normal looking assignment operator a = 1.

我对R不熟悉,我读过的所有东西都说过它通常比箭头赋值运算符a <- 1要优于普通的赋值运算符a = 1。

This was fine until I tried using the transform() function, where I noticed the assignment failed to actually occur.

这在我尝试使用transform()函数之前是没问题的,我注意到赋值实际上没有发生。

So if I try the following the sum_x and mean_x are not added to the data frame. If I were instead to try updating an existing variable on the data frame it would not update.

如果我尝试下面的sum_x和mean_x没有添加到数据帧中。如果我尝试更新数据框架上的现有变量,它将不会更新。

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 does work 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 so I know when I should be using each method of assignment so as to not run into an unexpected pitfall.

我想知道为什么会这样,所以我知道什么时候应该使用每一种分配方法,这样就不会遇到意外的陷阱。

1 个解决方案

#1


2  

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

您被告知更喜欢<- over =,因为在某些情况下,结果可能是不明确的。但是,这只适用于为变量赋值的情况。在你的例子中,你不是。

The equals = operator is used to assign values to function parameters.

equals =运算符用于为函数参数赋值。

The transform function is using the = syntax to allow you to modify the environment, but you are not directly assigning the results to those variables. transform is doing that for you and knows to do it because of the particular syntax you are using.

转换函数使用=语法允许您修改环境,但是您没有直接将结果分配给这些变量。transform是为您做的,并且知道它,因为您正在使用的特定语法。

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

诀窍就在于看别人的帮助。在这种情况下转换)并遵循它。

Adding an example to show why it matters:

增加一个例子来说明它的重要性:

mean(x = 1:5)

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

表示求1 2 3 4 5的均值。它为参数x赋1:5。

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 1:5 to it. This is then positionally matched with x.

有效,但不做你所期望的。没有参数a,所以它会创建一个变量a并给它赋1:5。然后它与x的位置匹配。

mean(a = 1:5)

doesn't work because there is no parameter called a in the mean function and the context makes R want to do parameter assignment.

它不能工作,因为在均值函数中没有称为a的参数,而上下文使R想要进行参数赋值。

#1


2  

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

您被告知更喜欢<- over =,因为在某些情况下,结果可能是不明确的。但是,这只适用于为变量赋值的情况。在你的例子中,你不是。

The equals = operator is used to assign values to function parameters.

equals =运算符用于为函数参数赋值。

The transform function is using the = syntax to allow you to modify the environment, but you are not directly assigning the results to those variables. transform is doing that for you and knows to do it because of the particular syntax you are using.

转换函数使用=语法允许您修改环境,但是您没有直接将结果分配给这些变量。transform是为您做的,并且知道它,因为您正在使用的特定语法。

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

诀窍就在于看别人的帮助。在这种情况下转换)并遵循它。

Adding an example to show why it matters:

增加一个例子来说明它的重要性:

mean(x = 1:5)

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

表示求1 2 3 4 5的均值。它为参数x赋1:5。

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 1:5 to it. This is then positionally matched with x.

有效,但不做你所期望的。没有参数a,所以它会创建一个变量a并给它赋1:5。然后它与x的位置匹配。

mean(a = 1:5)

doesn't work because there is no parameter called a in the mean function and the context makes R want to do parameter assignment.

它不能工作,因为在均值函数中没有称为a的参数,而上下文使R想要进行参数赋值。