Resorting dataframe with personal order

Hello everyone,

I´m actually looking for a function for sorting columns of a dataframe. I do need a personal order (e.g. not alphabetic). I do have different groups, each time measured several times – in total >50 columns.

Here is an example dataframe:
df <- data.frame(Value = rep(c(1:4)),
Patient1_1 = rep(c(1:4)), Patient2_1 = rep(c(1:4)), Control_1 = rep(c(1:4)),
Patient1_2 = rep(c(1:4)), Patient2_2 = rep(c(1:4)), Control_2 = rep(c(1:4)),
Patient1_3 = rep(c(1:4)), Patient2_3 = rep(c(1:4)), Control_3 = rep(c(1:4))

I tried by using the function below – to first only resort all the patients in single groups.
df_patient_grouping <- df[order(names(df))]

Next step should be final resorting patients according to my special order. For example first all the controls (from 1 to 3), next Patient2 (from 1 to 3), followed by Patient1 (from 1 to 3).
Should be: Control_1, Control_2, Control_3, Patient2_1, Patient2_2, Patient2_3, Patient1_1, Patient1_2, Patient1_3

Of course I could use the select function (rstatix) but I do not want to add column names each time I will have a new round of measurement.
Is there a possibility to define the order (without the numeric label)?

Thanks in advance.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.