Stats raving mad

The blog

Uh!

by M. Parzakonis on February 21, 2010

Didn’t know this…

a<-structure(c(25,34,12,5),.Names=c("0","2","4","7+"))
> data
 0  2  4 7+
25 34 12  5

It’s becoming clear that I have learned R in the most unstructured way…I always do it in two stages :ashamed:

> data<-c(25,34,12,5)
> names(data)<-c("0","2","4","7+")
> data
 0  2  4 7+
25 34 12  5

It’s really useful to wrap it all in a single function.

Attribute Specification

Description:

 ‘structure’ returns the given object with further attributes
 set.

Usage:

 structure(.Data, ...)

Arguments:

 .Data: an object which will have various attributes attached to it.

 ...: attributes, specified in ‘tag=value’ form, which will be
 attached to data.

Details:

 Adding a class ‘"factor"’ will ensure that numeric codes are
 given integer storage mode.

 For historical reasons (these names are used when deparsing),
 attributes ‘".Dim"’, ‘".Dimnames"’, ‘".Names"’,
 ‘".Tsp"’ and ‘".Label"’ are renamed to ‘"dim"’,
 ‘"dimnames"’, ‘"names"’, ‘"tsp"’ and ‘"levels"’.

 It is possible to give the same tag more than once, in which case
 the last value assigned wins.  As with other ways of assigning
 attributes, using ‘tag=NULL’ removes attribute ‘tag’ from
 ‘.Data’ if it is present.

14 thoughts on “Uh!

  1. Tal Galili says:

    Cool, I didn’t know this. Thanks for sharing!

    Tal

  2. romunov says:

    Mighty useful!

    Where do you generally learn to code? Books, on-line, mentor?

  3. romunov says:

    Any books/links you could recommend?

  4. efrique says:

    Well, I’d have used

    a a a
    counts
    0 2 4 7+
    25 34 12 5

  5. efrique says:

    Sorry, the code and much of the comment seems to have all got eaten. I’ve not seen that happen before.

    I was pointing out that you can achieve the same thing with the array command, and that you can also name the dimension easily.

  6. Bob Muenchen says:

    Since you’re used to names(a), I would just do it as:

    a<-structure(c(25,34,12,5), names=c("0","2","4","7+"))

    That is, use "names=" rather than ".Names=". I don't really know the historical reason that lets ".Names=" work.

    Cheers,
    Bob

    • Of course! This is what I (and probably most useRs) will use inside structure().This will be more consistent with other parts of code we have written.

      I searched but I couldn;t find the historical reason, either.

      PS : I like your book so much. I used it the reverse way. I learned a little SAS ;)

  7. Bob Muenchen says:

    Glad you liked the book. It took me forever to write. Ralph O’Brien says that in a few years there will be so many students graduating knowing mainly R that I’ll need to write, “SAS for R Users.” That’ll be the day!

  8. efrique :

    Sorry, the code and much of the comment seems to have all got eaten. I’ve not seen that happen before.

    I was pointing out that you can achieve the same thing with the array command, and that you can also name the dimension easily.

    I get the array thing you tried to show. I don’t get however what happened to your code ;(

  9. etiennebr says:

    dput() allows to display the structure() code of an object so you can recreate it. This is sometimes used on mailing list to load data. Maybe it can be useful.
    > dput(matrix(1:9,3))
    structure(1:9, .Dim = c(3L, 3L))
    > dput(data.frame(a=1,b=4,e=6))
    structure(list(a = 1, b = 4, e = 6), .Names = c(“a”, “b”, “e”
    ), row.names = c(NA, -1L), class = “data.frame”)

  10. So we dived into dusty things I guess…

  11. Pingback: Accessing your file system from within R « YAWN

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">