[R] ordering columns (longitudinal data in wide format)

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 2 17:26:00 CEST 2006


Assuming this data:

s <- structure(list(L.qol.0 = 83, L.qol.0.08 = 86, L.qol.0.17 = 89,
    L.qol.0.25 = 92, L.qol.0.5 = 91, L.qol.0.42 = 87, L.qol.0.34 = 90),
   .Names = c("L.qol.0", "L.qol.0.08", "L.qol.0.17", "L.qol.0.25",
   "L.qol.0.5", "L.qol.0.42", "L.qol.0.34"),
   class = "data.frame", row.names = "1")

# we can sort it by column names like this:
s[,sort(names(s))]

# also note that mixed sort in gtools can sort by numeric
# value in mixed character/numeric names which gives the
# same result here but may not in different examples
library(gtools)
s[,mixedsort(names(s))]


On 8/2/06, z.dalton at lancaster.ac.uk <z.dalton at lancaster.ac.uk> wrote:
> Hi,
>
> I am working on some longitudinal data in wide format and I am having a problem ordering the data columns.  To expand, a subset of what I am working on is as follows;
>
> >s
>   L.qol.0 L.qol.0.08 L.qol.0.17 L.qol.0.25 L.qol.0.5 L.qol.0.42 L.qol.0.34
> 1      83         86         89         92        91         87         90
>
> >names(s)
> [1] "L.qol.0"    "L.qol.0.08" "L.qol.0.17" "L.qol.0.25" "L.qol.0.5"
> [6] "L.qol.0.42" "L.qol.0.34"
>
> # in this object s (not a vector), 'L.qol' is measured at time points 0, 0.08, 0.17, 0.25, 0.34, 0.42 and 0.5.  As you can see, however, the time points are not in the correct order in object s.  Does anyone know how to order these column names along with their corresponding measurements?  Clearly s[order(s)] does not work since this just orders the corresponding measurements.
>
> I would be extremely grateful for any help on this matter, it may be really simple, but I have tried for ages.
>
> Thank you,
>
> Zoe
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list