[R] dataframe transposition

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Oct 13 12:40:49 CEST 1999


Jan.Schelling at hydro.com writes:


> to check the structure of the object I would recommend str() instead of page():
> 
> str(wk)

page() or, more to the point, dput() does have the advantage of
providing the full story (if you eval the result, you get the original
structure). It is handy for people trying to come up with suggestions 
that they can run their code on your data easily. Str() is often
better to analyse the structure, though.

> ----------------------
> 
> mlapsley at ndirect.co.uk on 99-10-13 12:21 AM GMT
...
> Dear R-helpers,
> 
> > wk
>   authorisor count(*)
> 1        JMC      4
> 2        LCA      259
> 3        MCM      136
> 4        MDH      266
> 5        ML       27
> 6        SA       1
> >
> >page(wk)
> structure(list(authorisor = c("JMC", "LCA", "MCM", "MDH", "ML",
> "SA"), "count(*)" = c("4", "259", "136", "266", "27", "1")),
>  .Names =c("authorisor",
> "count(*)"), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")
...
> > as.data.frame(rbind(c(4,259,136,266,27,1)))->wk2
> > names(wk2)<-dnames
> > wk2
>   JMC LCA MCM MDH ML SA
> 1   4 259 136 266 27  1

This is not quite comparable to what you'd expect since the original
was mode "character"...

Note that all that is really different in your example is
that the vectors that make up the data frame are named:

You get

structure(list(JMC = structure("4  ", .Names = "count(*)"), LCA =
structure("259", .Names = "count(*)"), 
MCM = structure("136", .Names = "count(*)"), MDH = structure("266",
 .Names= "count(*)"), 
ML = structure("27 ", .Names = "count(*)"), SA = structure("1  ", .Names =
"count(*)")), .Names = c("JMC", 
"LCA", "MCM", "MDH", "ML", "SA"), row.names = "count(*)", class = "data.frame")

whereas you might expect

structure(list(JMC = "4  ", LCA = "259", ....

I'm not quite sure why as.data.frame does that (looks a bit like a
bug), but I can't really see it doing any harm?

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list