Assignment in R language

后端 未结 3 2025
刺人心
刺人心 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:23

    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
    

提交回复
热议问题