[R] Assignment of structures on a given environment]

Rita Ribeiro rita at liacc.up.pt
Wed Oct 17 12:31:24 CEST 2001


Robert Gentleman wrote:

> On Tue, Oct 16, 2001 at 04:40:34PM +0100, Rita Ribeiro wrote:
> > Hi,
> >
> > In order to avoid deep copies by passing large arguments to
functions or
> >
> > returning values, I'm trying to do the assignment of variables in a
> > given environment. The problem is when I try to assign a structure:
a
> > list for example.
> >
> > If I have:
> >     ind <- c("a","b")
> >
> > my idea is doing something like
> >
> >     l <- alist()
> >     l[ind] <- as.list(c(20,40))
> >
> >  in a given environment.
> >
> >
> >
> > Example:
> >     ref <- new.env()
> >     (.....)
> >     assign("l",alist(),env=ref)
> >
> >
> >     If I do
> >         assign("l$a",20,env=ref)
> >
> >     it creates me a new variable in the ref environment  named "l$a"

> >
>   This is what you asked it to do.
>   Environments work at the level of whole objects. I'm not sure what
>   it would mean for l$a to exist in ref if l doesn't exist in ref.
>   So, you just assign l into the environment and retrieve it from the
>   environment, using get.
>
>   If you you have only one list then perhaps you do not want to put
>   the list in, rather just the elements.
>
>    names <- names(l)
>    for(i in 1:length(l)) assign(names[i], l[[i]], env=ref)
>
>    you then have a and b, for example, in the environment and can
>    extract them.
>
>    If you have two lists, l1 and l2 and both have a's and b's as
>    components then you have to put in the whole list (or adopt a
>    naming convention).
>

    Hi, again


    My problem is  concerned with the fact that my list will be a list
of lists
which  elements will be sucessively appended and defined.
    I want to "build" the whole list into the environment. preserving
its structure
(it will be useful for me to be able to manipulate the list later).
    More precisely, I want to do ,in a given environment, something
like:

            l  <- alist()

            l$first <- alist()
            l$first[ind] <- as.list(c(20,40))  # where ind is local
vector
containind the elements names

            l$second <- 50

            So If i do

            > l
            $first
            $first$a
            [1] 20

            $first$b
            [1] 40


            $second
            [1] 50





            and

            > l$first$b
            [1] 40


            I'm searching for some alternative to solution to  define
this hierarchy
in the designed environment, without needing to use the 'evalq'
command..... Is
there one?

            Thanks again,

            Rita


>
>    One might ask whether you are sure that you need to do this?
>    I work on very large data sets and copying has pretty much never
>    been an issue.
>
> >
> >
> >     So,  I did:
> >
> >     eval(l <- alist(), env=ref)
> >
> >     but this creates the l list both on the current and on the ref
> > environment.
> >
> >
> >     The alternative solution that I found out was:
> >
> >     evalq(l<-alist(),env=ref)
> >
> >     and then
> >
> >         evalq(ind <-c("a","b"), env=ref)
> >         evalq(l[ind] <- as.list(c(20,40)), env=ref)
> >
> >
> > I would like to know if there is another possible solution, instead
of
> > doing these 'evalqs' along the program code.
> >
> >
> > Thanks,
> >
> >     Rita
> >
> >
> >
> >
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

> > 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
> >
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

>
> --
>
+---------------------------------------------------------------------------+

> | Robert Gentleman                 phone : (617)
632-5250                   |
> | Associate Professor              fax:   (617)
632-2444                   |
> | Department of Biostatistics      office: M1B28
> | Harvard School of Public Health  email:
rgentlem at jimmy.dfci.harvard.edu   |
>
+---------------------------------------------------------------------------+

>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-

> 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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



-------------- next part --------------
An embedded message was scrubbed...
From: Rita Ribeiro <rita at liacc.up.pt>
Subject: Re: [R] Assignment of structures on a given environment
Date: Wed, 17 Oct 2001 11:26:42 +0100
Size: 5022
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20011017/e982c9f4/attachment.mht


More information about the R-help mailing list