Re: [R] Rule for accessing attributes?

From: Tribo Laboy <tribolaboy_at_gmail.com>
Date: Thu, 27 Mar 2008 17:54:36 +0900

Thanks, this really solved my problem.

Actually, I do have a good introduction to R - a book co-authored by some W.N. Venables and some B.D. Ripley, colloquially called 'MASS' is on my desk. I find it really very helpful. Still, as it is a book on statistics, some details on R are only mentioned in passing. I also use some other books and online resources, but obviously will need to delve a bit deeper. One of the reasons I am moving to R is because it has a great user community. I hope I am not abusing it with too many questions.

Regards,

TL

On Thu, Mar 27, 2008 at 5:16 PM, Prof Brian Ripley <ripley_at_stats.ox.ac.uk> wrote:
> Use [[ ]].
>
> It seems it is time for you to study a good introduction to R.
>
>
>
>
> On Thu, 27 Mar 2008, Tribo Laboy wrote:
>
> > Now, how is it that I can access the contents of a named list by
> > dynamically computed name?
> >
> > To go back to my previous example I have a list and I know the names.
> > Now I want do something with that named data in a loop.
> >
> > lst <- list(x = 1:3, y = 4:6, z = 7:9)
> > nm <-names(lst)
> > nm
> > [1] "x" "y" "z"
> >
> > I can access the list elements by name directly:
> >
> > lst$x; lst$y; lst$z,
> >
> > But I want to do
> >
> > for (k in 1:3) {
> > lst$nm[k]
> > }
> >
> > But this doesn't work, basically because
> > lst$nm[1] returns a NULL.
> >
> > So what do I do?
> >
> > Thanks for helping,
> >
> > TL
> >
> >
> > On Thu, Mar 27, 2008 at 4:30 PM, Tribo Laboy <tribolaboy_at_gmail.com> wrote:
> >> So am I to understand that the only realy _correct_ and _recommended_
> >> way of accessing the attributes is through
> >>
> >> attr(someobject, "attributename") ?
> >>
> >>
> >> Regards,
> >>
> >> TL
> >>
> >>
> >>
> >> On Thu, Mar 27, 2008 at 4:16 PM, Prof Brian Ripley
> >> <ripley_at_stats.ox.ac.uk> wrote:
> >> > Oh please don't recommend misuse of @ to those already confused.
> >> >
> >> > @ is for accessing slots in S4 objects. This 'works' because they happen
> >> > to be stored as attributes. See the help page (and the warning that it
> >> > does no checking - we may change that).
> >> >
> >> > Similarly,
> >> >
> >> > plt$title <- "My Title"
> >> >
> >> > works because the package maintainer (of ggplot2, unmentioned?) has chosen
> >> > to set things up that way. R is very flexible, and there is plenty of
> >> > scope for package authors to do confusing things.
> >> >
> >> >
> >> >
> >> > On Thu, 27 Mar 2008, Christos Hatzis wrote:
> >> >
> >> > > You need to use the '@' operator to directly access attributes (not
> >> > > elements) of objects:
> >> > >
> >> > >> lst_at_names
> >> > > [1] "x" "y" "z"
> >> > >
> >> > > See ?'@' for more details.
> >> > >
> >> > > -Christos
> >> > >
> >> > >> -----Original Message-----
> >> > >> From: r-help-bounces_at_r-project.org
> >> > >> [mailto:r-help-bounces_at_r-project.org] On Behalf Of Tribo Laboy
> >> > >> Sent: Thursday, March 27, 2008 12:16 AM
> >> > >> To: r-help_at_r-project.org
> >> > >> Subject: [R] Rule for accessing attributes?
> >> > >>
> >> > >> Hi !
> >> > >>
> >> > >> I am a new user and quite confused by R-indexing.
> >> > >>
> >> > >> Make a list and get the attributes
> >> > >> lst <- list(x = 1:3, y = 4:6, z = 7:9)
> >> > >> attributes(lst)
> >> > >>
> >> > >> This returns:
> >> > >>
> >> > >> $names
> >> > >> [1] "x" "y" "z"
> >> > >>
> >> > >> I can easily do:
> >> > >>
> >> > >> nm <-names(lst)
> >> > >>
> >> > >> or
> >> > >>
> >> > >> nm <-attr(lst,"names")
> >> > >>
> >> > >> which both return the assigned names of the named list 'lst',
> >> > >> but why then this doesn't work:
> >> > >>
> >> > >> lst$names
> >> > >>
> >> > >> ?
> >> > >>
> >> > >> I am confused ... Moreover, I noticed that some of the objects (e.g.
> >> > >> plot objects returned by ggplot) also have attributes when
> >> > >> queried by the 'attributes' function, but they are accessible
> >> > >> by the $ notation.
> >> > >> (e.g.
> >> > >>
> >> > >> xydf <- data.frame(x = 1:5, y = 11:15)
> >> > >> plt <- ggplot(data = xydf, aes(x = x,y = y)) + geom_point()
> >> > >> attributes(plt)
> >> > >>
> >> > >> Now we can change the title:
> >> > >>
> >> > >> plt$title <- "My Title"
> >> > >> plt
> >> > >>
> >> > >> So is it some inconsistency or am I missing something important?
> >> > >>
> >> > >> ______________________________________________
> >> > >> R-help_at_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.
> >> > >>
> >> > >>
> >> > >
> >> > > ______________________________________________
> >> > > R-help_at_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.
> >> > >
> >> >
> >> > --
> >> > 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
> >> >
> >>
> >
>
> --
>
>
> 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
>



R-help_at_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. Received on Thu 27 Mar 2008 - 08:58:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 09:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive