R:+=(加等于)和++ (加加)相当于 c++/c#/java 等?

发布于 2024-11-02 18:27:17 字数 73 浏览 6 评论 0原文

R 是否像 c++/c#/其他语言一样有 +=(加等于)或 ++(加加)的概念?

Does R have a concept of += (plus equals) or ++ (plus plus) as c++/c#/others do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

哆兒滾 2024-11-09 18:27:17

不,不是,请参阅:R 语言定义:运算符< /a>

洋洋洒洒 2024-11-09 18:27:17

按照@GregaKešpret,您可以创建一个中缀运算符:

`%+=%` = function(e1,e2) eval.parent(substitute(e1 <- e1 + e2))
x = 1
x %+=% 2 ; x

Following @GregaKešpret you can make an infix operator:

`%+=%` = function(e1,e2) eval.parent(substitute(e1 <- e1 + e2))
x = 1
x %+=% 2 ; x
小兔几 2024-11-09 18:27:17

R 没有增量运算符的概念(例如 C 中的 ++)。然而,自己实现一个并不困难,例如:

inc <- function(x)
{
 eval.parent(substitute(x <- x + 1))
}

在这种情况下,您会调用

x <- 10
inc(x)

但是,它会引入函数调用开销,因此它比键入 x <- x + 1 自己。如果我没记错的话,引入增量运算符是为了使编译器的工作更容易,因为它可以将代码直接转换为那些机器语言指令。

R doesn't have a concept of increment operator (as for example ++ in C). However, it is not difficult to implement one yourself, for example:

inc <- function(x)
{
 eval.parent(substitute(x <- x + 1))
}

In that case you would call

x <- 10
inc(x)

However, it introduces function call overhead, so it's slower than typing x <- x + 1 yourself. If I'm not mistaken increment operator was introduced to make job for compiler easier, as it could convert the code to those machine language instructions directly.

只是我以为 2024-11-09 18:27:17

R 没有这些操作,因为 R 中的(大多数)对象是不可变的。它们不会改变。通常,当您看起来像是在修改对象时,实际上是在修改副本。

R doesn't have these operations because (most) objects in R are immutable. They do not change. Typically, when it looks like you're modifying an object, you're actually modifying a copy.

狼性发作 2024-11-09 18:27:17

递增和递减 10。

require(Hmisc)
inc(x) <- 10 

dec(x) <- 10

Increment and decrement by 10.

require(Hmisc)
inc(x) <- 10 

dec(x) <- 10
椒妓 2024-11-09 18:27:17

我们发布了一个包,ropers,来帮助解决这类问题。您可以在这里阅读更多相关信息: https ://happylittlescripts.blogspot.com/2018/09/make-your-r-code-nicer-with-roperators.html

install.packages('roperators')
require(roperators)

x <- 1:3
x %+=% 1; x
x %-=% 3; x
y <- c('a', 'b', 'c')
y %+=% 'text'; y
y %-=% 'text'; y

# etc

We released a package, roperators, to help with this kind of thing. You can read more about it here: https://happylittlescripts.blogspot.com/2018/09/make-your-r-code-nicer-with-roperators.html

install.packages('roperators')
require(roperators)

x <- 1:3
x %+=% 1; x
x %-=% 3; x
y <- c('a', 'b', 'c')
y %+=% 'text'; y
y %-=% 'text'; y

# etc
酒与心事 2024-11-09 18:27:17

我们可以覆盖+。如果使用一元 + 并且其参数本身就是一元 + 调用,则在调用环境中递增相关对象。

`+` <- function(e1,e2){
  # if binary `+`, keep original behavior
  if(!missing(e2)) return(base::`+`(e1, e2))
  
  # if inner call isn't unary `+` called on language object,
  # keep original behavior
  inner_call <- substitute(e1)
  inner_call_is_plus_on_lng <-
    length(inner_call) == 2 &&
    identical(inner_call[[1]], quote(`+`)) &&
    is.language(inner_call[[2]])
  if(!inner_call_is_plus_on_lng) return(base::`+`(e1))
  
  eval.parent(substitute(X <- X + 1, list(X = inner_call[[2]])))
}

x <- 10

++x
x
#> [1] 11

其他操作不会改变:

x + 2
#> [1] 13
x ++ 2
#> [1] 13
+x
#> [1] 11
x
#> [1] 11

我真的不能推荐它,因为你弄乱了出于某种原因而优化的原语。

We can override +. If unary + is used and its argument is itself an unary + call, then increment the relevant object in the calling environment.

`+` <- function(e1,e2){
  # if binary `+`, keep original behavior
  if(!missing(e2)) return(base::`+`(e1, e2))
  
  # if inner call isn't unary `+` called on language object,
  # keep original behavior
  inner_call <- substitute(e1)
  inner_call_is_plus_on_lng <-
    length(inner_call) == 2 &&
    identical(inner_call[[1]], quote(`+`)) &&
    is.language(inner_call[[2]])
  if(!inner_call_is_plus_on_lng) return(base::`+`(e1))
  
  eval.parent(substitute(X <- X + 1, list(X = inner_call[[2]])))
}

x <- 10

++x
x
#> [1] 11

other operations don't change :

x + 2
#> [1] 13
x ++ 2
#> [1] 13
+x
#> [1] 11
x
#> [1] 11

I can't really recommend it since you're messing with primitives which are optimised for a reason.

贱人配狗天长地久 2024-11-09 18:27:17

我们还可以使用inplace

library(inplace)
x <- 1
x %+<-% 2

We can also use inplace

library(inplace)
x <- 1
x %+<-% 2
剩余の解释 2024-11-09 18:27:17

如果你想在数组中使用i++来增加索引,你可以尝试i <- i + 1,例如,

k = 0
a = 1:4
for (i in 1:4) 
    cat(a[k <- k + 1], " ")
# 1 2 3 4

但这里<- 不能替换为 =,这不会更新索引,

k = 0
a = 1:4
for (i in 1:4) 
    cat(a[k = k + 1], " ")
# 1 1 1 1

因为 =<- 并不总是等效的,正如 ?`<-` 中所述

If you want to use i++ in an array to increment the index, you can try i <- i + 1, for example,

k = 0
a = 1:4
for (i in 1:4) 
    cat(a[k <- k + 1], " ")
# 1 2 3 4

but here <- can NOT be replaced with =, which does not update the index,

k = 0
a = 1:4
for (i in 1:4) 
    cat(a[k = k + 1], " ")
# 1 1 1 1

since = and <- are not always equivalent, as said in ?`<-`

紫竹語嫣☆ 2024-11-09 18:27:17

这是一种增加变量的更简单的方法。

inc = 1

for(i in 1:10){
    print(paste("Value of inc: ", inc))
    inc = sum(c(inc, 1))
}

您将得到如下结果

[1] "Value of inc:  1"
[1] "Value of inc:  2"
[1] "Value of inc:  3"
[1] "Value of inc:  4"
[1] "Value of inc:  5"
[1] "Value of inc:  6"
[1] "Value of inc:  7"
[1] "Value of inc:  8"
[1] "Value of inc:  9"
[1] "Value of inc:  10"

Here is a simpler approach to increment a variable.

inc = 1

for(i in 1:10){
    print(paste("Value of inc: ", inc))
    inc = sum(c(inc, 1))
}

You will get results as follows

[1] "Value of inc:  1"
[1] "Value of inc:  2"
[1] "Value of inc:  3"
[1] "Value of inc:  4"
[1] "Value of inc:  5"
[1] "Value of inc:  6"
[1] "Value of inc:  7"
[1] "Value of inc:  8"
[1] "Value of inc:  9"
[1] "Value of inc:  10"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文