What are Replacement Functions in R?

When you call

cutoff(x) <- 65

you are in effect calling

x <- "cutoff<-"(x = x, value = 65)

The name of the function has to be quoted as it is a syntactically valid but non-standard name and the parser would interpret <- as the operator not as part of the function name if it weren’t quoted.

"cutoff<-"() is just like any other function (albeit with a weird name); it makes a change to its input argument on the basis of value (in this case it is setting any value in x greater than 65 to Inf (infinite)).

The magic is really being done when you call the function like this

cutoff(x) <- 65

because R is parsing that and pulling out the various bits to make the real call shown above.

More generically we have

FUN(obj) <- value

R finds function "FUN<-"() and sets up the call by passing obj and value into "FUN<-"() and arranges for the result of "FUN<-"() to be assigned back to obj, hence it calls:

obj <- "FUN<-"(obj, value)

A useful reference for this information is the R Language Definition Section 3.4.4: Subset assignment ; the discussion is a bit oblique, but seems to be the most official reference there is (replacement functions are mentioned in passing in the R FAQ (differences between R and S-PLUS), and in the R language reference (various technical issues), but I haven’t found any further discussion in official documentation).

Leave a Comment