Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Replacement Functions in R?

Tags:

r

I searched for a reference to learn about replacement functions in R, but I haven't found any yet. I'm trying to understand the concept of the replacement functions in R. I have the code below but I don't understand it:

"cutoff<-" <- function(x, value){  x[x > value] <- Inf  x  } 

and then we call cutoff with:

 cutoff(x) <- 65 

Could anyone explain what a replacement function is in R?

like image 317
Sam Avatar asked Jul 19 '12 14:07

Sam


People also ask

How replace function is used?

Returns a String in which a specified substring has been replaced with another substring a specified number of times. Required. String expression containing substring to replace.

How do I replace a value in a vector in R?

To replace a value in an R vector, we can use replace function. It is better to save the replacement with a new object, even if you name that new object same as the original, otherwise the replacements will not work with further analysis.

How do I find and replace in R code?

Find and Replace RStudio supports finding and replacing text within source documents: Find and replace can be opened using the Ctrl+F shortcut key, or from the Edit -> Find... menu item.


2 Answers

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).

like image 106
Gavin Simpson Avatar answered Sep 23 '22 15:09

Gavin Simpson


Gavin provides an excellent discussion of the interpretation of the replacement function. I wanted to provide a reference since you also asked for that: R Language Definition Section 3.4.4: Subset assignment.

like image 31
Brian Diggs Avatar answered Sep 25 '22 15:09

Brian Diggs