[R] Re order variables in a dataframe
Simon Blomberg
s.blomberg1 at uq.edu.au
Thu May 21 05:23:32 CEST 2009
Alphabetically, like this?:
> dat <- data.frame(d=rnorm(3), c=rnorm(3), b=rnorm(3), a=rnorm(3))
> dat
d c b a
1 -0.1816733 -0.4106008 -0.2855991 -1.022951
2 -1.8326818 -0.6515208 0.3344884 -2.191836
3 1.0924867 0.1159611 -1.3409719 1.195545
> dat2 <- dat[,order(names(dat))]
> dat2
a b c d
1 -1.022951 -0.2855991 -0.4106008 -0.1816733
2 -2.191836 0.3344884 -0.6515208 -1.8326818
3 1.195545 -1.3409719 0.1159611 1.0924867
>
Cheers,
Simon.
On Wed, 2009-05-20 at 20:19 -0700, pgseye wrote:
> This is no doubt a very basic question for most R users, but is there an easy
> way to reorder the variables (columns) in a dataframe (I can't seem to find
> an answer anywhere). I've generally been creating a new dataframe and
> selecting the new order I want from the old but this is time-consuming.
>
> Thanks,
>
> Paul
--
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
School of Biological Sciences
The University of Queensland
St. Lucia Queensland 4072
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
http://www.uq.edu.au/~uqsblomb
email: S.Blomberg1_at_uq.edu.au
Policies:
1. I will NOT analyse your data for you.
2. Your deadline is your problem.
The combination of some data and an aching desire for
an answer does not ensure that a reasonable answer can
be extracted from a given body of data. - John Tukey.
More information about the R-help
mailing list