R语言中的赋值 [英] Assignment in R language

查看:56
本文介绍了R语言中的赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 R 语言中的赋值是如何工作的.

I am wondering how assignment works in the R language.

考虑以下 R shell 会话:

Consider the following R shell session:

> x <- c(5, 6, 7)
> x[1] <- 10
> x
[1] 10 6 7
>

我完全理解.向量 (5, 6, 7) 被创建并绑定到符号x".后来,'x' 被反弹到新的向量 (10, 6, 7) 因为向量是不可变的数据结构.

which I totally understand. The vector (5, 6, 7) is created and bound to the symbol 'x'. Later, 'x' is rebound to the new vector (10, 6, 7) because vectors are immutable data structures.

但是这里发生了什么:

> c(4, 5, 6)[1] <- 10
Error in c(4, 5, 6)[1] <- 10 :
  target of assignment expands to non-language object
>

或在这里:

> f <- function() c(4, 5, 6)
> f()[1] <- 10
Error in f()[1] <- 10 : invalid (NULL) left side of assignment
>

在我看来,只能为命名数据结构(如x")赋值.

It seems to me that one can only assign values to named data structures (like 'x').

我问的原因是因为我尝试实现R语言核心但我不确定如何处理这样的任务.

The reason why I am asking is because I try to implement the R language core and I am unsure how to deal with such assignments.

提前致谢

推荐答案

在我看来只能为命名数据结构(如x")赋值.

It seems to me that one can only assign values to named data structures (like 'x').

这正是 ?"<-" 的文档所说的:

That's precisely what the documentation for ?"<-" says:

说明:

 Assign a value to a name.

x[1] <- 10 不使用与 x <- c(5, 6, 7) 相同的函数.前者调用[<-,后者调用<-.

x[1] <- 10 doesn't use the same function as x <- c(5, 6, 7). The former calls [<- while the latter calls <-.

这篇关于R语言中的赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆