[R] Column order in stacking/unstacking

Peter Ehlers ehlers at ucalgary.ca
Sat Mar 12 12:09:24 CET 2011


On 2011-03-12 02:01, Dennis Murphy wrote:
> Hi:
>
> It would help if you named your variables such that alphanumeric ordering
> doesn't disturb your variable ordering. Having been burned by this a few
> times, I've learned the basics of sprintf() :) Here's your example
> revisited, along with an alternative stacking/unstacking display with
> package reshape.
>
> library(reshape)
> vnames<- paste('X', sprintf('%02d', c(1, 2, 10)), sep = '')
>> vnames
> [1] "X01" "X02" "X10"

<snip>

You can do this a bit more simply with sprintf:

  vnames <- sprintf("%s%02d", "X", c(1, 2, 10))

Peter Ehlers



More information about the R-help mailing list