I followed the discussion over HERE and am curious why is using<<-
frowned upon in R. What kind of confusion will it cause?
我跟着这里的讨论,很好奇为什么使用<< - 在R中不赞成。它会引起什么样的困惑?
I also would like some tips on how I can avoid <<-
. I use the following quite often. For example:
我也想了解如何避免<< - 的一些提示。我经常使用以下内容。例如:
### Create dummy data frame of 10 x 10 integer matrix.
### Each cell contains a number that is between 1 to 6.
df <- do.call("rbind", lapply(1:10, function(i) sample(1:6, 10, replace = TRUE)))
What I want to achieve is to shift every number down by 1, i.e all the 2s will become 1s, all the 3s will be come 2 etc. Therefore, all n
would be come n-1
. I achieve this by the following:
我想要实现的是将每个数字减少1,即所有2s将变为1,所有3将变为2等等。因此,所有n都将变为n-1。我通过以下方式实现这一目标:
df.rescaled <- df
sapply(2:6, function(i) df.rescaled[df.rescaled == i] <<- i-1))
In this instance, how can I avoid <<-
? Ideally I would want to be able to pipe the sapply results into another variable along the lines of:
在这种情况下,我怎样才能避免<< - ?理想情况下,我希望能够将sapply结果传递到另一个变量:
df.rescaled <- sapply(...)
2 个解决方案
#1
16
First point
第一点
<<-
is NOT the operator to assign to global variable. It tries to assign the variable in the nearest parent environment. So, say, this will make confusion:
<< - 不是分配给全局变量的运算符。它尝试在最近的父环境中分配变量。所以,比如说,这会让人感到困惑:
f <- function() {
a <- 2
g <- function() {
a <<- 3
}
}
then,
然后,
> a <- 1
> f()
> a # the global `a` is not affected
[1] 1
Second point
第二点
You can do that by using Reduce
:
你可以使用Reduce来做到这一点:
Reduce(function(a, b) {a[a==b] <- a[a==b]-1; a}, 2:6, df)
or apply
或申请
apply(df, c(1, 2), function(i) if(i >= 2) {i-1} else {i})
But
但
simply, this is sufficient:
简单来说,这就足够了:
ifelse(df >= 2, df-1, df)
#2
5
You can think of <<-
as global assignment (approximately, because as kohske points out it assigns to the top environment unless the variable name exists in a more proximal environment). Examples of why this is bad are here:
您可以将<< - 视为全局赋值(大约,因为正如kohske指出的那样,除非变量名存在于更近端的环境中,否则它将分配给顶层环境)。这里不好的原因如下:
Examples of the perils of globals in R and Stata
R和Stata中全局变量的危险例子
#1
16
First point
第一点
<<-
is NOT the operator to assign to global variable. It tries to assign the variable in the nearest parent environment. So, say, this will make confusion:
<< - 不是分配给全局变量的运算符。它尝试在最近的父环境中分配变量。所以,比如说,这会让人感到困惑:
f <- function() {
a <- 2
g <- function() {
a <<- 3
}
}
then,
然后,
> a <- 1
> f()
> a # the global `a` is not affected
[1] 1
Second point
第二点
You can do that by using Reduce
:
你可以使用Reduce来做到这一点:
Reduce(function(a, b) {a[a==b] <- a[a==b]-1; a}, 2:6, df)
or apply
或申请
apply(df, c(1, 2), function(i) if(i >= 2) {i-1} else {i})
But
但
simply, this is sufficient:
简单来说,这就足够了:
ifelse(df >= 2, df-1, df)
#2
5
You can think of <<-
as global assignment (approximately, because as kohske points out it assigns to the top environment unless the variable name exists in a more proximal environment). Examples of why this is bad are here:
您可以将<< - 视为全局赋值(大约,因为正如kohske指出的那样,除非变量名存在于更近端的环境中,否则它将分配给顶层环境)。这里不好的原因如下:
Examples of the perils of globals in R and Stata
R和Stata中全局变量的危险例子