Assignment in R language

六眼飞鱼酱① 提交于 2019-12-03 23:54:50
Joshua Ulrich

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

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

Description:

 Assign a value to a name.

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

Ari B. Friedman

As per @Owen's answer to this question, x[1] <- 10 is really doing two things. It is calling the [<- function, and it is assigning the result of that call to x.

So what you want to achieve your c(4, 5, 6)[1] <- 10 result is:

> `[<-`(c(4, 5, 6),1, 10)
[1] 10  5  6
42-

You can make modifications to anonymous functions, but there is no assignment to anonymous vectors. Even R creates temporary copies with names and you will sometimes see error messages that reflect that fact. You can read this in the R language definition on page 21 where it deals with the evaluation of expressions for "subset assignment" and for other forms of assignment:

x[3:5] <- 13:15 
# The result of this commands is as if the following had been executed 
`*tmp*` <- x 
x <- "[<-"(`*tmp*`, 3:5, value=13:15) 
rm(`*tmp*`) 

And there is a warning not to use *tmp* as an object name because it would be overwritting during the next call to [<-

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!