R赋值运算符

标签 r

我正在检查 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/

相关文章:

r - 使用ggplot boxplot进行位置躲避警告?

r - LaTeX 符号未正确转换为 Moodle

r - 动态计算 r 中的条件表达式

将 1 列中的多个值替换为 R 中的单个值

r - Shiny 的如何刷新服务器功能之前加载的数据

r - 在 rvest 中获取和设置 cookie

r - 在ggplot2之上覆盖基本R图形

r - 嵌套小标题、执行操作、以不同形式取消嵌套和嵌套数据

r - 在R中,read_csv()解析失败: Converting integers into NA's

r - 分面后缺少 ggpubr 括号