Home > statistics > Uh!

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.
Advertisements
Categories: statistics Tags: , ,
  1. February 21, 2010 at 10:33 am

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

    Tal

  2. February 21, 2010 at 2:45 pm

    Mighty useful!

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

    • February 21, 2010 at 9:43 pm

      All of them. Generally I read other people’s code & read blogs 😉

  3. February 21, 2010 at 9:45 pm

    Any books/links you could recommend?

    • February 21, 2010 at 10:43 pm

      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
    February 22, 2010 at 3:57 am

    Well, I’d have used

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

  5. efrique
    February 22, 2010 at 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. February 22, 2010 at 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

    • February 22, 2010 at 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. February 22, 2010 at 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. February 22, 2010 at 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. etiennebr
    February 23, 2010 at 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. February 23, 2010 at 9:04 pm

    So we dived into dusty things I guess…

  1. March 7, 2010 at 10:48 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: