[R] What is the most efficient way to assign to PARTS of objects in other frames/environments?
Thomas Lumley
tlumley at u.washington.edu
Fri May 17 17:43:36 CEST 2002
On Fri, 17 May 2002 oehl_list at gmx.de wrote:
> Can please someone familiar with the R internals explain on the following:
>
> PR#1434 from r-bugs clarifies that
>
> assign("a[1]", x, SomeOtherFrame)
>
> or
>
> assign("a$a", x, SomeOtherFrame)
>
> will NOT assign to an object 'a' in the other frame BUT create a new object
> called 'a[1]' resp. 'a$a'.
>
> This leads to the following question: what is the most efficient way to
> assign to PARTS of objects in other frames/environments?
>
> Any way I know appears to have some problems associated:
>
> # doing it locally involves several calls and probably copies the whole
> object
> local.copy <- get("a", SomeOtherFrame)
> local.copy$a <- x
> assign("a", local.copy, SomeOtherFrame)
>
> # using eval fails because x might be unknown in the other frame
> > t1 <- function(){
> + # x is locally defined
> + x <- 9
> + eval(parse(text="a$a <- x"), envir=globalenv())
> + }
> > t1()
> Error in eval(expr, envir, enclos) : Object "x" not found
>
Instead of
assign("a$a",x,envir=SomeOtherFrame)
you can do
eval(substitute(a$a<-localx,localx=x),envir=SomeOtherFrame)
-thomas
Thomas Lumley Asst. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list