Replace columns with canonical method after mutate_each or summaryise

Take the following example.

library(dplyr)
temp <- data.frame(lapply(1:3, function(i) rnorm(5, 0, 1)))
names(temp) <- paste0("X", 1:3)

temp_each <-
    temp %>%
    mutate_each(funs(mean, median))

Studying the names temp_each, we see that

> names(temp_each)
[1] "X1"        "X2"        "X3"        "X1_mean"   "X2_mean"   "X3_mean"   "X1_median" "X2_median" "X3_median"

that is, the final columns are in groups of three, always ordered X1, X2, X3+ applicable function.

However, I would like it to look like

[1] "X1"        "X1_mean"   "X1_median" "X2"        "X2_mean"   "X2_median" "X3"        "X3_mean"   "X3_median"

Does anyone know how to implement this, preferably using dplyr, for a data frame with many columns and arbitrary column names?

+4
source share
3 answers

Here you can use mixedorderfromgtools

library(gtools)
temp_each[,mixedorder(colnames(temp_each))]

#           X1    X1_mean  X1_median         X2    X2_mean  X2_median
#1  0.28285115 -0.4369067 0.08556155 -0.9402162 -0.9857593 -0.7676634
#2 -1.29193398 -0.4369067 0.08556155 -0.5442052 -0.9857593 -0.7676634
#3 -1.42261044 -0.4369067 0.08556155 -0.7676634 -0.9857593 -0.7676634
#4  0.16159810 -0.4369067 0.08556155 -2.2270920 -0.9857593 -0.7676634
#5  0.08556155 -0.4369067 0.08556155 -0.4496198 -0.9857593 -0.7676634
#           X3   X3_mean   X3_median
#1  0.04606554 0.0923336 -0.08168136
#2 -0.08168136 0.0923336 -0.08168136
#3  0.90535333 0.0923336 -0.08168136
#4 -0.15699052 0.0923336 -0.08168136
#5 -0.25107897 0.0923336 -0.08168136
+5
source

With the base, Ryou can try the following:

> temp_each[order(colnames(temp_each))]
#          X1    X1_mean X1_median         X2   X2_mean  X2_median         X3  X3_mean X3_median
#    1  0.4142743 -0.4389318 -0.285517  1.8662158 0.3534017 -0.2308971  1.3593561 0.478106 0.6306579
#    2 -0.8031115 -0.4389318 -0.285517 -0.2308971 0.3534017 -0.2308971 -0.6160166 0.478106 0.6306579
#    3 -1.8729143 -0.4389318 -0.285517  1.0171626 0.3534017 -0.2308971  0.2634524 0.478106 0.6306579
#    4  0.3526097 -0.4389318 -0.285517 -0.6378480 0.3534017 -0.2308971  0.6306579 0.478106 0.6306579
#    5 -0.2855170 -0.4389318 -0.285517 -0.2476247 0.3534017 -0.2308971  0.7530800 0.478106 0.6306579
+2
source

Thanks to everyone for the answers using base Ranyway.

This is my preferred base solution R.

old_names <- names(temp)
new_names <- unlist(lapply(old_names, function(old_name) paste0(old_name, c("_mean", "_median"))))
temp_each <- temp_each[new_names]

However, I have now developed how to do this dplyr, using a standard score, from this answer: Group by several columns in dplyr using input string input

He's pretty confused.

temp <- data.frame(lapply(1:3, function(i) rnorm(5, 0, 1)))
names(temp) <- paste0("X", 1:3)

old_names <- names(temp)
new_names <- unlist(lapply(old_names, function(old_name) paste0(old_name, c("_mean", "_median"))))


temp_each <-
    temp %>%
    mutate_each(funs(mean, median)) %>%
    select_(.dots = lapply(new_names, as.symbol))
+1
source

All Articles