Assignment in R language

后端 未结 3 2096
刺人心
刺人心 2021-02-19 09:54

I am wondering how assignment works in the R language.

Consider the following R shell session:

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


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-19 10:28

    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 [<-

提交回复
热议问题