Uh! 14

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. Reply Tal Galili Feb 21, 2010 10:33 am

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

    Tal

  2. Reply romunov Feb 21, 2010 2:45 pm

    Mighty useful!

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

  3. Reply romunov Feb 21, 2010 9:45 pm

    Any books/links you could recommend?

  4. Reply efrique Feb 22, 2010 3:57 am

    Well, I’d have used

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

  5. Reply efrique Feb 22, 2010 3:59 am

    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. Reply Bob Muenchen Feb 22, 2010 4:33 am

    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

    • Reply Manos Parzakonis Feb 22, 2010 12:07 pm

      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. Reply Bob Muenchen Feb 22, 2010 3:18 pm

    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. Reply Manos Parzakonis Feb 22, 2010 8:38 pm

    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. Reply etiennebr Feb 23, 2010 8:49 pm

    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. Reply Manos Parzakonis Feb 23, 2010 9:04 pm

    So we dived into dusty things I guess…

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

Leave a Reply