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.
Cool, I didn’t know this. Thanks for sharing!
Tal
Mighty useful!
Where do you generally learn to code? Books, on-line, mentor?
All of them. Generally I read other people’s code & read blogs
Any books/links you could recommend?
I would say I learned alot from Faraway’s “Practical Regression & ANOVA with R”.
For blogs visit R bloggers and browse thru posts.
Well, I’d have used
a a a
counts
0 2 4 7+
25 34 12 5
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.
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
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!
I get the array thing you tried to show. I don’t get however what happened to your code ;(
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”)
So we dived into dusty things I guess…
Pingback: Accessing your file system from within R « YAWN