I have a data.frame in long form. I want to reshape to wide, but I care about the ordering of the columns.
Here is a toy example. In reality, I will have many more groups (grp
).
# Minimum working example
mwe <- "grp mean sd label
1 C90 90 19 Heart_rate
2 D20 86 18 Heart_rate
3 H09 80 16 Heart_rate
9 Z89 89 18 Heart_rate"
# Read in the text data
dd <- read.table(text=mwe, header=TRUE)
I tried this solution without success.
dcast(setDT(dd), label~grp, value.var=list("mean", "sd"), sep="_" )
label mean_C90 mean_D20 mean_H09 mean_Z89 sd_C90 sd_D20 sd_H09 sd_Z89
1: Heart_rate 90 86 80 89 19 18 16 18
What I want are the columns after label
ordered by the grp
then by mean
and sd
... like this
label mean_C90 sd_C90 mean_D20 sd_D20 mean_H09 sd_H09 mean_Z89 sd_Z89
I am stuck!