[R] <-

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 14 13:38:15 CEST 2003


These are called `replacement functions', or sometimes `assignment
functions'.  Leaving aside S4 methods (set by setReplaceMethod), they all
end in "<-", as the first quoted example is actually evaluated as 
something very close to

x <- "substring<-"(x, 1, 1, toupper(substring(x, 1, 1)))

In the base package there are

> ls(NULL, patt="<-$")
 [1] "$<-"                "<-"                 "<<-"
 [4] "[<-"                "[[<-"               "attr<-"
 [7] "attributes<-"       "body<-"             "class<-"
[10] "codes<-"            "colnames<-"         "comment<-"
[13] "contrasts<-"        "diag<-"             "dim<-"
[16] "dimnames<-"         "environment<-"      "formals<-"
[19] "is.na<-"            "length<-"           "levels<-"
[22] "mode<-"             "mostattributes<-"   "names<-"
[25] "oldClass<-"         "parent.env<-"       "row.names<-"
[28] "rownames<-"         "split.data.frame<-" "split<-"
[31] "storage.mode<-"     "substr<-"           "substring<-"
[34] "tsp<-"

(and some methods; "split.data.frame<-" is a mistake and codes<- is 
deprecated in R-devel).

But users can write them as well, and there are some in the methods 
package.

On Wed, 14 May 2003, Damon Wischik wrote:

> 
> Uwe Ligges wrote:
> > Perl experts might do it differently, but the "R way" seems to be
> >   substring(x, 1, 1) <- toupper(substring(x, 1, 1))
> >   substring(x, 2) <- tolower(substring(x, 2))
> 
> I hadn't come across this use of substring on the left hand side of <-
> before. Now, of course, I see it explained on the help page for substr.
> 
> What other special functions can appear on the left hand side of <-? Is
> there anywhere I can look to find a list? I've come across
>   vector[vector of logicals] <- 
>   vector[vector of integers] <- 
>   data.frame[] <-
> 
> One thing I would find very handy is a shortcut for
>   res <- myfunc()
>   a <- res$val1
>   b <- res$val2
> Something along the lines of
>   list(a=val1,b=val2) <- myfunc()
> but I don't know what the right syntax would be or how I'd go about
> programming it. Any suggestions?
> 
> Damon Wischik.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list