[R] Transpose a dataframe

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jun 28 18:41:19 CEST 2002


On Fri, 28 Jun 2002, Jonathan Baron wrote:

> On 06/28/02 17:57, Patrick Hausmann wrote:
> >Hello,
> >
> >I would like to switch this dataframe:
> >> k
> >    country  1960  1961  1962  1963
> >99      ARG  7493  7733  7581  7108
> >246     AUS 10484 10342 10809 11357
> >295     AUT  7438  7808  7938  8212
> >393     BDI   587   502   555   608
> >442     BEL  8223  8638  9021  9311
> >
> >in this structure:
> >
> >year	ARG	AUS	AUT	BDI	BEL
> >1960	7493	10484	7438	587	8223
> >1961	7733	10342	7808	502	8638
> >1962	7581	10809	7938	555	9021
> >1963	7108	11357	8212	608	9311
> >
> >I'm trying the 'reshape' and 't' command, but still failed.
>
> t() likes to have a matrix, not a data frame.  So you can say
> something like
>
> as.data.frame(t(as.matrix(data.frame(MY.DATA.FRAME))))

But country is a character column that needs to become the var names.  So

row.names(k) < k$country

as.data.frame(t(as.matrix(k[-1])))

seems about right.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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