[R] within: order of newly added variables

RINNER Heinrich HEINRICH.RINNER at tirol.gv.at
Wed Sep 2 15:27:36 CEST 2009


Dear R community,

I am using function 'within' in R.2.9.1 to add variables to an existing data.frame. This works wonderful, except for one minor point: The new variables are added to the data in reverse order.

For example:
x <- data.frame(a = 1:3, b = 4:6)
y <- within(x, {
     c = a^2
     d = b^2
     e = c+d
     }
)
gives
  a b  e  d c
1 1 4 17 16 1
2 2 5 29 25 4
3 3 6 45 36 9

Just what I want; except that I would prefer the columns to be in order a,b,c,d,e instead.

I could use transform ("transform(x, c=a^2, d=b^2, e=c+d)"), which preserves the specified order of variables, but that won't work here because unfortunately it doesn't find object 'd' (same with "within(x, {e = c+d; d = b^2; c = a^2})", by the way).

Of course in my toy example I can easily do something like y[, c(1:2,5:3)] afterwards, but I'd like to ask if maybe there is a simple way to make 'within' preserve the order of specified variables (or maybe someone can shed light on why this is not possible?).

Thanks,
Heinrich.




More information about the R-help mailing list