我正在检查 R 赋值运算符并试图了解它们是如何工作的,所以我编写了以下代码:
> somevar1 <- "stuff1"
> "stuff2" -> somevar2
> somevar3 = "stuff3"
> "stuff4" <- somevar4
Error: object 'somevar4' not found
> "stuff5" = somevar5
Error: object 'somevar5' not found
> `<-`(somevar6, "stuff6")
> `=`(somevar7, "stuff7")
> `=`("stuff8", somevar8)
Error: object 'somevar8' not found
> `->`("stuff9", somevar9)
Error in `->`("stuff9", somevar9) : could not find function "->"
> `<<-`(somevar10, "stuff10")
> `->>`("stuff11", somevar11)
Error in `->>`("stuff11", somevar11) : could not find function "->>"
到目前为止,在使用 somevar9
的地方,一切似乎都符合预期。
但是,我不明白为什么会出现 Error in
->("stuff9", somevar9) : could not find function "->"
和->>
("stuff11", somevar11) 中的错误:找不到函数“->>”
。 ->
和 ->>
运算符不应该存在它们吗?提前致谢!
最佳答案
当 R 解析器遇到 a -> b
它调用 '<-'("b", a)
当遇到 a ->> b
它调用 '<<-'("b", a)
如果我们执行以下操作,我们可以明确地看到这一点:
as.call(quote(a <- 1))
#> a <- 1
as.call(quote(a <<- 1))
#> a <<- 1
as.call(quote(1 -> a))
#> a <- 1
as.call(quote(1 ->> a))
#> a <<- 1
由 reprex package 于 2022-01-27 创建(v2.0.1)
关于R赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70879706/