[R] attach

0 views
Skip to first unread message

Christophe Dutang

unread,
Oct 14, 2009, 1:21:18 AM10/14/09
to r-h...@r-project.org
Hi all,

I have a question regarding the memory usage for the attach function.
Say I have a data.frame inputdat that I create with read.csv.

I would like to know what happens on the memory side when I use
attach(inputdata)

Is there a second allocation of memory for inputdata?

Then I'm using eval on a expression which depends on the columns of
inputdata. Is it better not to use attach function?

Thanks in advance

Christophe


--
Christophe Dutang
Ph.D. student at ISFA, Lyon, France
website: http://dutangc.free.fr

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Dieter Menne

unread,
Oct 14, 2009, 2:40:45 AM10/14/09
to r-h...@r-project.org


Christophe Dutang1 wrote:
>
>
> I would like to know what happens on the memory side when I use
> attach(inputdata)
>
> Is there a second allocation of memory for inputdata?
>

Not, it just guides the syntax.


Christophe Dutang1 wrote:
>
> Is it better not to use attach function?
>

A qualified "yes" in the sense of "do not use it". I think it is used to
much in old documentation, presumably because some S eggshells.

I use with() if I have a nasty formula to unclutter; it acts locally only
and you don't get unwanted side effects.

Dieter


--
View this message in context: http://www.nabble.com/attach-tp25885494p25886141.html
Sent from the R help mailing list archive at Nabble.com.

Peter Dalgaard

unread,
Oct 17, 2009, 4:11:43 AM10/17/09
to Dieter Menne, r-h...@r-project.org
Dieter Menne wrote:
>
>
> Christophe Dutang1 wrote:
>>
>> I would like to know what happens on the memory side when I use
>> attach(inputdata)
>>
>> Is there a second allocation of memory for inputdata?
>>
>
> Not, it just guides the syntax.

Wrong. There's a virtual copy of data plus a conversion from data frame
to attached environment on the search path. If you modify data in either
of the original frame or the environment, you will get duplication.

>
> Christophe Dutang1 wrote:
>> Is it better not to use attach function?
>>
>
> A qualified "yes" in the sense of "do not use it". I think it is used to
> much in old documentation, presumably because some S eggshells.
>
> I use with() if I have a nasty formula to unclutter; it acts locally only
> and you don't get unwanted side effects.

Well, opinions vary. Having to qualify data frame access on each use
does tend to get in the way of explorative work, at least it does so for
me.

--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dal...@biostat.ku.dk) FAX: (+45) 35327907

Bernardo Rangel Tura

unread,
Oct 18, 2009, 4:42:11 AM10/18/09
to r-help
On Wed, 2009-10-14 at 07:21 +0200, Christophe Dutang wrote:
> Hi all,
>
> I have a question regarding the memory usage for the attach function.
> Say I have a data.frame inputdat that I create with read.csv.
>
> I would like to know what happens on the memory side when I use
> attach(inputdata)
>
> Is there a second allocation of memory for inputdata?
>
> Then I'm using eval on a expression which depends on the columns of
> inputdata. Is it better not to use attach function?
>
> Thanks in advance
>
> Christophe

Well, if you attach a data.frame twice times, it use your memory twice
times.

I don't use attach I prefer with(data.frame, command)
--
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil

Reply all
Reply to author
Forward
0 new messages