[R] Making a Subset Assignment function ("[<-")

dj hawthorne djthorne at stanford.edu
Thu Jun 28 11:39:25 CEST 2012


Greetings,
    I am trying to understand how to making a subset assignment function.
Take the example where of making a "last" generic function.

last <- function(x,...) UseMethod("last");
last.numeric <- function(x, ...) x[length(x)] 
last.list <- function(x, ...) x[[length(x)]] 
.....

Now we run into the problem at hand if we want to assign something to the
last item. Of course we could define a separate setter function, but I am
not interested in this problem specifically, but understanding the method
that would let me write the function to allow the following reasonable
behavior.

vec <- 1:5
last(vec)<-10
vec $ 10


Thank you for your time and expertise!

--
View this message in context: http://r.789695.n4.nabble.com/Making-a-Subset-Assignment-function-tp4634717.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list