How about to insert a variable a2 inbetween the first and second columns of dat:
dat2 <- cbind(dat[,1], a2=a2, dat[,2:3]) Where a2 is the new variable. This mangles the variable name for column 1, unfortunately. Surely someone else will offer a better solution. Simon. On Wed, 2009-05-20 at 20:39 -0700, pgseye wrote: > Thanks Simon, > > I should have explained myself better (although I didn't know about the > order function so that's handy information). > > I've found a few times, I've added variables to a dataframe that I would > like to 'group' with variables earlier in the datset. So I was wondering if > you could somehow ask R, for example, to take that last variable and insert > it at a different position in the dataframe. > > Thanks, > > Paul > > Simon Blomberg-4 wrote: > > > > 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. > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > > -- 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. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.