Uh!

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.



Comments

  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?

    1. All of them. Generally I read other people’s code & read blogs ;)

  3. romunov says:

    Any books/links you could recommend?

    1. I would say I learned alot from Faraway’s “Practical Regression & ANOVA with R”.

      For blogs visit R bloggers and browse thru posts.

  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

    1. 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. [...] Next step is not strictly needed, but I just like to have a dataframe with only the two columsn with the file name and size. I create the dataframe and column names in one go using the ’structure’ function (I found out about this option here). [...]