Filip Filip - 1 month ago 12
R Question

How does assignment into function return value work?

I have stumbled apon this code:

colnames(dataFrame) <- c("firstCol", "secondCol")


which renames columns of given dataframe dataFrame. What is happening there? How can I possibly assign something to return value?

Corresponding R-fiddle

Answer

Example 1:

> `setvalue<-` <- function(x,value) value
> x <- 0
> setvalue(x) <- 1
> x
[1] 1

Example 2:

> `add<-` <- function(x,value) x+value
> x
[1] 1
> add(x) <- 3      # x <- x+3
> x
[1] 4
Comments