Replacement Function in R

Replacement Function:

In a replacement function, you must make sure that the last two characters of the name are <-, and usually, that means you will need to enclose the name in double quotes to prevent the evaluator from attempting to carry out an explicit assignment.

You must also make sure that the return value is the modified copy of the object you want to modify. R will automatically rewrite the code to carry out an assignment, so the return value must be appropriate for that operation. If it is not, no error will be signaled but the result will probably not be useful. The last argument to a replacement function must be named value and it will be matched to the value of the right-hand side of the assignment.

Example:

> names(x) = letters[1:4]
> names(x)
[1] "a" "b" "c" "d"
> x = "names<-"(x, LETTERS[1:4]) > x
A B C D
1 10 3 4