[R] Problem with data.frames

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Aug 4 13:31:28 CEST 2003


"Simon Fear" <Simon.Fear at synequanon.com> writes:

> Peter is right, but there is a point here: it would be nice if
> attach(my.dframe) would do nothing - or at least warn - if my.dframe was
> already in the search list. Like library(). Attaching twice is almost
> bound
> to be an error?

The problem is, how can you tell? Consider

f <- function() {
    d <- data.frame(x=1:10,y=rnorm(10))
    attach(d)
}
f()
f()

This attaches two *different* dataframes, both called "d".


> Of course in many circumstances it might be better to use with() than
> attach() - if you haven't come across with(), it works like attach()
> with an
> inbuilt detach() when the parentheses close.

I do occasionally speculate whether it would be possible to change
attach() to be more like with(), but it's tricky business.

> Aside 1: it would also be nice if ?attach pointed to ?with. Is this kind
> of
> suggestion best sent to r-help or r-devel?

R-devel, in principle, but developers will pick up hints from all
over. R-bugs can also be used if you want to make sure that a
suggestion is recorded (it may drown in the spam for a while,
though...) R-bugs is best used for clear bugs, but a well-defined
enhancement request can also get in there (do remember to label
it as such, or it might generate a host of "Not a bug" responses.)

> Aside 2: is with() efficient, or does it create a copy of the dataset? 

It converts the data frame to an environment, so I would suspect that
it has to copy, but these things are "black magic": The paradigm in R
is that of a functional language, where assignment merely attaches a
label to computed objects. Objects are generally copied rather than
modified in-place, but to avoid too much inefficiency there are a lot
of special cases where we try to eliminate copying if possible.


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list