在摘要R中声明更改变量名称?

时间:2018-11-28 14:46:23

标签: r dplyr tidyverse

给出一个数据框df:

structure(list(train_user_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), train_idx = c(38L, 38L, 91L, 91L, 78L, 85L, 38L, 
38L, 100L, 38L), test_user_id = c(2L, 7L, 1L, 1L, 3L, 4L, 2L, 
2L, 4L, 2L), test_idx = c(7L, 1487L, 267L, 205L, 416L, 1228L, 
5L, 3L, 1221L, 26L), isSame = c(FALSE, FALSE, TRUE, TRUE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE), D_manhattan_scaled_rank = c(1, 
1, 1, 1, 1, 1, 2, 2, 1, 2), D_cosinus_rank = c(2, 2, 2, 2, 2, 
2, 2, 2, 2, 2), D_manhattan_scaled = c(0.0657286768232851, 0.0826741765405462, 
0.0940875224861761, 0.097775476943595, 0.10376335813558, 0.145577989872983, 
0.146052068454132, 0.148619857868654, 0.168290231579623, 0.176975987030052
), D_manhattan_scaled_norm_median = c(-1.00671374438998, -0.997385542790249, 
-0.991102695209199, -0.989072540699541, -0.985776316696353, -0.962758092319792, 
-0.962497120341812, -0.961083597121844, -0.950255399985173, -0.945474043327689
), D_manhattan_scaled_norm_standard = c(-1.48819317015296, -1.4760112732926, 
-1.46780636901353, -1.46515514706224, -1.46085053803271, -1.43079054927003, 
-1.43044974041366, -1.42860379037803, -1.41446301744423, -1.4082189420958
), D_manhattan_scaled_adj_std = c(0.0430204530981368, 0.0460635656366407, 
0.0324184592356798, 0.0970173017224344, 0.0753537776638339, 0.109856813487073, 
0.0574451159458192, 0.0579062455845995, 0.0704125398622902, 0.0629985056910741
), D_manhattan_scaled_adj_std_norm_median = c(-0.503188246554804, 
-0.501396401264777, -0.509430911762538, -0.47139382631581, -0.484149740879808, 
-0.463833665989625, -0.494694717690257, -0.494423195371945, -0.487059240140704, 
-0.491424771169117), D_manhattan_scaled_adj_std_norm_standard = c(-1.48257160625723, 
-1.47883448831024, -1.49559146702019, -1.41626035765178, -1.44286441633519, 
-1.40049269568096, -1.46485728720877, -1.4642909933806, -1.44893254172815, 
-1.45803740387515), D_manhattan = c(0.672887300807173, 0.675930413345677, 
0.610974280121391, 2.50949211962699, 1.1051786119143, 1.58216223895939, 
0.687311963654855, 0.687773093293636, 0.974947980462479, 0.69286535340011
), D_manhattan_norm_median = c(-0.954621945729433, -0.954491537063699, 
-0.957275148434567, -0.875916613319857, -0.936096658748905, -0.915656140170372, 
-0.954003795394266, -0.953984034277921, -0.941677524612532, -0.953765812034198
), D_manhattan_norm_standard = c(-1.4490608751289, -1.44889327448836, 
-1.45247075951332, -1.3479091299438, -1.42525225821863, -1.39898219412758, 
-1.44826643105155, -1.4482410341522, -1.43242476234849, -1.44796057588953
), D_euclidean = c(0.518771643675271, 0.5188097254424, 0.53063275623408, 
1.92924997316828, 0.966420157224431, 1.44718873459025, 0.51911028560706, 
0.519127719117536, 0.686153917573155, 0.519347425186172), D_euclidean_norm_median = c(-0.90357471715071, 
-0.903572834076855, -0.902988206790553, -0.833829138991541, -0.881439315466942, 
-0.857666187097651, -0.903557971925472, -0.903557109870209, -0.89529796931099, 
-0.903546245806251), D_euclidean_norm_standard = c(-1.44257769296322, 
-1.44257514625199, -1.44178448322374, -1.34825221188084, -1.41264128024888, 
-1.38048996930628, -1.44255504634578, -1.44255388048295, -1.43138403391866, 
-1.44253918767967), D_cosinus = c(0.721004237529186, 0.721004237529186, 
1.2063547475627e-05, 3.04804894533106e-05, 0.00015538105717583, 
0.000465547437121572, 0.721004237529186, 0.721004237529186, 0.000173984509277303, 
0.721004237529186), D_cosinus_norm_median = c(24.0734598908976, 
24.0734598908976, -0.0269862437287224, -0.0263706246774829, -0.022195600990392, 
-0.0118277379227909, 24.0734598908976, 24.0734598908976, -0.0215737475064442, 
24.0734598908976), D_cosinus_norm_standard = c(6.67802092714532, 
6.67802092714532, -0.248881727244683, -0.248704787232191, -0.24750481016543, 
-0.244524899428419, 6.67802092714532, 6.67802092714532, -0.247326078264596, 
6.67802092714532), D_manhattan_scaled_median = c(1.89451285226621, 
1.89451285226621, 1.89451285226621, 1.89451285226621, 1.89451285226621, 
1.89451285226621, 1.89451285226621, 1.89451285226621, 1.89451285226621, 
1.89451285226621), D_manhattan_scaled_mean = c(2.13586416008507, 
2.13586416008507, 2.13586416008507, 2.13586416008507, 2.13586416008507, 
2.13586416008507, 2.13586416008507, 2.13586416008507, 2.13586416008507, 
2.13586416008507), D_manhattan_scaled_std = c(1.3910394999656, 
1.3910394999656, 1.3910394999656, 1.3910394999656, 1.3910394999656, 
1.3910394999656, 1.3910394999656, 1.3910394999656, 1.3910394999656, 
1.3910394999656), D_manhattan_scaled_percentile_90 = c(3.71110091976209, 
3.71110091976209, 3.71110091976209, 3.71110091976209, 3.71110091976209, 
3.71110091976209, 3.71110091976209, 3.71110091976209, 3.71110091976209, 
3.71110091976209), D_manhattan_scaled_adj_std_median = c(0.897591141115348, 
0.897591141115348, 0.897591141115348, 0.897591141115348, 0.897591141115348, 
0.897591141115348, 0.897591141115348, 0.897591141115348, 0.897591141115348, 
0.897591141115348), D_manhattan_scaled_adj_std_mean = c(1.25026954403403, 
1.25026954403403, 1.25026954403403, 1.25026954403403, 1.25026954403403, 
1.25026954403403, 1.25026954403403, 1.25026954403403, 1.25026954403403, 
1.25026954403403), D_manhattan_scaled_adj_std_std = c(0.814293951024465, 
0.814293951024465, 0.814293951024465, 0.814293951024465, 0.814293951024465, 
0.814293951024465, 0.814293951024465, 0.814293951024465, 0.814293951024465, 
0.814293951024465), D_manhattan_scaled_adj_std_percentile_90 = c(2.59590324174216, 
2.59590324174216, 2.59590324174216, 2.59590324174216, 2.59590324174216, 
2.59590324174216, 2.59590324174216, 2.59590324174216, 2.59590324174216, 
2.59590324174216), D_manhattan_median = c(22.9491830993431, 22.9491830993431, 
22.9491830993431, 22.9491830993431, 22.9491830993431, 22.9491830993431, 
22.9491830993431, 22.9491830993431, 22.9491830993431, 22.9491830993431
), D_manhattan_mean = c(26.9833793358661, 26.9833793358661, 26.9833793358661, 
26.9833793358661, 26.9833793358661, 26.9833793358661, 26.9833793358661, 
26.9833793358661, 26.9833793358661, 26.9833793358661), D_manhattan_std = c(18.1569266596329, 
18.1569266596329, 18.1569266596329, 18.1569266596329, 18.1569266596329, 
18.1569266596329, 18.1569266596329, 18.1569266596329, 18.1569266596329, 
18.1569266596329), D_manhattan_percentile_90 = c(46.2843849540569, 
46.2843849540569, 46.2843849540569, 46.2843849540569, 46.2843849540569, 
46.2843849540569, 46.2843849540569, 46.2843849540569, 46.2843849540569, 
46.2843849540569), D_euclidean_median = c(18.7919380759661, 18.7919380759661, 
18.7919380759661, 18.7919380759661, 18.7919380759661, 18.7919380759661, 
18.7919380759661, 18.7919380759661, 18.7919380759661, 18.7919380759661
), D_euclidean_mean = c(22.0900856850802, 22.0900856850802, 22.0900856850802, 
22.0900856850802, 22.0900856850802, 22.0900856850802, 22.0900856850802, 
22.0900856850802, 22.0900856850802, 22.0900856850802), D_euclidean_std = c(14.9533118019418, 
14.9533118019418, 14.9533118019418, 14.9533118019418, 14.9533118019418, 
14.9533118019418, 14.9533118019418, 14.9533118019418, 14.9533118019418, 
14.9533118019418), D_euclidean_percentile_90 = c(39.0151316707499, 
39.0151316707499, 39.0151316707499, 39.0151316707499, 39.0151316707499, 
39.0151316707499, 39.0151316707499, 39.0151316707499, 39.0151316707499, 
39.0151316707499), D_cosinus_median = c(0.000819387628734736, 
0.000819387628734736, 0.000819387628734736, 0.000819387628734736, 
0.000819387628734736, 0.000819387628734736, 0.000819387628734736, 
0.000819387628734736, 0.000819387628734736, 0.000819387628734736
), D_cosinus_mean = c(0.0259171161436376, 0.0259171161436376, 
0.0259171161436376, 0.0259171161436376, 0.0259171161436376, 0.0259171161436376, 
0.0259171161436376, 0.0259171161436376, 0.0259171161436376, 0.0259171161436376
), D_cosinus_std = c(0.104085795622488, 0.104085795622488, 0.104085795622488, 
0.104085795622488, 0.104085795622488, 0.104085795622488, 0.104085795622488, 
0.104085795622488, 0.104085795622488, 0.104085795622488), D_cosinus_percentile_90 = c(0.0307355215440245, 
0.0307355215440245, 0.0307355215440245, 0.0307355215440245, 0.0307355215440245, 
0.0307355215440245, 0.0307355215440245, 0.0307355215440245, 0.0307355215440245, 
0.0307355215440245), experiment = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1)), .Names = c("train_user_id", "train_idx", "test_user_id", 
"test_idx", "isSame", "D_manhattan_scaled_rank", "D_cosinus_rank", 
"D_manhattan_scaled", "D_manhattan_scaled_norm_median", "D_manhattan_scaled_norm_standard", 
"D_manhattan_scaled_adj_std", "D_manhattan_scaled_adj_std_norm_median", 
"D_manhattan_scaled_adj_std_norm_standard", "D_manhattan", "D_manhattan_norm_median", 
"D_manhattan_norm_standard", "D_euclidean", "D_euclidean_norm_median", 
"D_euclidean_norm_standard", "D_cosinus", "D_cosinus_norm_median", 
"D_cosinus_norm_standard", "D_manhattan_scaled_median", "D_manhattan_scaled_mean", 
"D_manhattan_scaled_std", "D_manhattan_scaled_percentile_90", 
"D_manhattan_scaled_adj_std_median", "D_manhattan_scaled_adj_std_mean", 
"D_manhattan_scaled_adj_std_std", "D_manhattan_scaled_adj_std_percentile_90", 
"D_manhattan_median", "D_manhattan_mean", "D_manhattan_std", 
"D_manhattan_percentile_90", "D_euclidean_median", "D_euclidean_mean", 
"D_euclidean_std", "D_euclidean_percentile_90", "D_cosinus_median", 
"D_cosinus_mean", "D_cosinus_std", "D_cosinus_percentile_90", 
"experiment"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

我正在构建一个基于我选择的D_type进行总结的函数。例如,我在选择

D_type <- "D_manhatten_scaled"

我要执行以下操作:

df %>% 
summarize(paste0(D_type, "_mean_standard") =- mean(D_type))

换句话说,如何声明根据D_type改变的summarize变量名,而D_type始终是不同的,而无需使用rename_at函数。

例如,此处的摘要变量将为“ D_manhatten_scaled_mean_standard”

在其他情况下,它可以是“ D_cosinus_mean_standard”。

请告知。

2 个答案:

答案 0 :(得分:1)

说实话,我不是100%确定我得到了你想要的东西。

但这是我想你想要的:

custom_summary <- function(df, col, ext, fun){
  col_name <- paste0(col, ext)
  value <- fun(df[[col]])
  df_out <- data_frame(value)
  names(df_out) <- eval(col_name)
  return(df_out)}

custom_summary(df = my_df, col = "D_manhattan_scaled", ext = "_mean_standard", fun = mean)
# # A tibble: 1 x 1
#      D_manhattan_scaled_mean_standard
#                                <dbl>
#   1                            0.123

答案 1 :(得分:1)

如果要使用dplyrtidyverse做这种事情,则需要一些整洁的评估。我建议阅读this

library(tidyverse)

df <-
  structure(
    list(
      train_user_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
                        1L, 1L, 1L),
      train_idx = c(38L, 38L, 91L, 91L, 78L, 85L, 38L,
                    38L, 100L, 38L),
      test_user_id = c(2L, 7L, 1L, 1L, 3L, 4L, 2L,
                       2L, 4L, 2L),
      test_idx = c(7L, 1487L, 267L, 205L, 416L, 1228L,
                   5L, 3L, 1221L, 26L),
      isSame = c(FALSE, FALSE, TRUE, TRUE, FALSE,
                 FALSE, FALSE, FALSE, FALSE, FALSE),
      D_manhattan_scaled_rank = c(1,
                                  1, 1, 1, 1, 1, 2, 2, 1, 2),
      D_cosinus_rank = c(2, 2, 2, 2, 2,
                         2, 2, 2, 2, 2),
      D_manhattan_scaled = c(
        0.0657286768232851,
        0.0826741765405462,
        0.0940875224861761,
        0.097775476943595,
        0.10376335813558,
        0.145577989872983,
        0.146052068454132,
        0.148619857868654,
        0.168290231579623,
        0.176975987030052
      ),
      D_manhattan_scaled_norm_median = c(
        -1.00671374438998,
        -0.997385542790249,-0.991102695209199,
        -0.989072540699541,
        -0.985776316696353,
        -0.962758092319792,-0.962497120341812,
        -0.961083597121844,
        -0.950255399985173,
        -0.945474043327689
      ),
      D_manhattan_scaled_norm_standard = c(
        -1.48819317015296,
        -1.4760112732926,-1.46780636901353,
        -1.46515514706224,
        -1.46085053803271,
        -1.43079054927003,-1.43044974041366,
        -1.42860379037803,
        -1.41446301744423,
        -1.4082189420958
      ),
      D_manhattan_scaled_adj_std = c(
        0.0430204530981368,
        0.0460635656366407,
        0.0324184592356798,
        0.0970173017224344,
        0.0753537776638339,
        0.109856813487073,
        0.0574451159458192,
        0.0579062455845995,
        0.0704125398622902,
        0.0629985056910741
      ),
      D_manhattan_scaled_adj_std_norm_median = c(
        -0.503188246554804,-0.501396401264777,
        -0.509430911762538,
        -0.47139382631581,
        -0.484149740879808,-0.463833665989625,
        -0.494694717690257,
        -0.494423195371945,
        -0.487059240140704,-0.491424771169117
      ),
      D_manhattan_scaled_adj_std_norm_standard = c(
        -1.48257160625723,-1.47883448831024,
        -1.49559146702019,
        -1.41626035765178,
        -1.44286441633519,-1.40049269568096,
        -1.46485728720877,
        -1.4642909933806,
        -1.44893254172815,-1.45803740387515
      ),
      D_manhattan = c(
        0.672887300807173,
        0.675930413345677,
        0.610974280121391,
        2.50949211962699,
        1.1051786119143,
        1.58216223895939,
        0.687311963654855,
        0.687773093293636,
        0.974947980462479,
        0.69286535340011
      ),
      D_manhattan_norm_median = c(
        -0.954621945729433,
        -0.954491537063699,-0.957275148434567,
        -0.875916613319857,
        -0.936096658748905,
        -0.915656140170372,-0.954003795394266,
        -0.953984034277921,
        -0.941677524612532,
        -0.953765812034198
      ),
      D_manhattan_norm_standard = c(
        -1.4490608751289,
        -1.44889327448836,-1.45247075951332,
        -1.3479091299438,
        -1.42525225821863,
        -1.39898219412758,-1.44826643105155,
        -1.4482410341522,
        -1.43242476234849,
        -1.44796057588953
      ),
      D_euclidean = c(
        0.518771643675271,
        0.5188097254424,
        0.53063275623408,
        1.92924997316828,
        0.966420157224431,
        1.44718873459025,
        0.51911028560706,
        0.519127719117536,
        0.686153917573155,
        0.519347425186172
      ),
      D_euclidean_norm_median = c(
        -0.90357471715071,-0.903572834076855,
        -0.902988206790553,
        -0.833829138991541,
        -0.881439315466942,-0.857666187097651,
        -0.903557971925472,
        -0.903557109870209,
        -0.89529796931099,-0.903546245806251
      ),
      D_euclidean_norm_standard = c(
        -1.44257769296322,-1.44257514625199,
        -1.44178448322374,
        -1.34825221188084,
        -1.41264128024888,-1.38048996930628,
        -1.44255504634578,
        -1.44255388048295,
        -1.43138403391866,-1.44253918767967
      ),
      D_cosinus = c(
        0.721004237529186,
        0.721004237529186,
        1.2063547475627e-05,
        3.04804894533106e-05,
        0.00015538105717583,
        0.000465547437121572,
        0.721004237529186,
        0.721004237529186,
        0.000173984509277303,
        0.721004237529186
      ),
      D_cosinus_norm_median = c(
        24.0734598908976,
        24.0734598908976,
        -0.0269862437287224,
        -0.0263706246774829,
        -0.022195600990392,-0.0118277379227909,
        24.0734598908976,
        24.0734598908976,
        -0.0215737475064442,
        24.0734598908976
      ),
      D_cosinus_norm_standard = c(
        6.67802092714532,
        6.67802092714532,
        -0.248881727244683,
        -0.248704787232191,
        -0.24750481016543,-0.244524899428419,
        6.67802092714532,
        6.67802092714532,
        -0.247326078264596,
        6.67802092714532
      ),
      D_manhattan_scaled_median = c(
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621,
        1.89451285226621
      ),
      D_manhattan_scaled_mean = c(
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507,
        2.13586416008507
      ),
      D_manhattan_scaled_std = c(
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656,
        1.3910394999656
      ),
      D_manhattan_scaled_percentile_90 = c(
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209,
        3.71110091976209
      ),
      D_manhattan_scaled_adj_std_median = c(
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348,
        0.897591141115348
      ),
      D_manhattan_scaled_adj_std_mean = c(
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403,
        1.25026954403403
      ),
      D_manhattan_scaled_adj_std_std = c(
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465,
        0.814293951024465
      ),
      D_manhattan_scaled_adj_std_percentile_90 = c(
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216,
        2.59590324174216
      ),
      D_manhattan_median = c(
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431,
        22.9491830993431
      ),
      D_manhattan_mean = c(
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661,
        26.9833793358661
      ),
      D_manhattan_std = c(
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329,
        18.1569266596329
      ),
      D_manhattan_percentile_90 = c(
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569,
        46.2843849540569
      ),
      D_euclidean_median = c(
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661,
        18.7919380759661
      ),
      D_euclidean_mean = c(
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802,
        22.0900856850802
      ),
      D_euclidean_std = c(
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418,
        14.9533118019418
      ),
      D_euclidean_percentile_90 = c(
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499,
        39.0151316707499
      ),
      D_cosinus_median = c(
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736,
        0.000819387628734736
      ),
      D_cosinus_mean = c(
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376,
        0.0259171161436376
      ),
      D_cosinus_std = c(
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488,
        0.104085795622488
      ),
      D_cosinus_percentile_90 = c(
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245,
        0.0307355215440245
      ),
      experiment = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1)
    ),
    .Names = c(
      "train_user_id",
      "train_idx",
      "test_user_id",
      "test_idx",
      "isSame",
      "D_manhattan_scaled_rank",
      "D_cosinus_rank",
      "D_manhattan_scaled",
      "D_manhattan_scaled_norm_median",
      "D_manhattan_scaled_norm_standard",
      "D_manhattan_scaled_adj_std",
      "D_manhattan_scaled_adj_std_norm_median",
      "D_manhattan_scaled_adj_std_norm_standard",
      "D_manhattan",
      "D_manhattan_norm_median",
      "D_manhattan_norm_standard",
      "D_euclidean",
      "D_euclidean_norm_median",
      "D_euclidean_norm_standard",
      "D_cosinus",
      "D_cosinus_norm_median",
      "D_cosinus_norm_standard",
      "D_manhattan_scaled_median",
      "D_manhattan_scaled_mean",
      "D_manhattan_scaled_std",
      "D_manhattan_scaled_percentile_90",
      "D_manhattan_scaled_adj_std_median",
      "D_manhattan_scaled_adj_std_mean",
      "D_manhattan_scaled_adj_std_std",
      "D_manhattan_scaled_adj_std_percentile_90",
      "D_manhattan_median",
      "D_manhattan_mean",
      "D_manhattan_std",
      "D_manhattan_percentile_90",
      "D_euclidean_median",
      "D_euclidean_mean",
      "D_euclidean_std",
      "D_euclidean_percentile_90",
      "D_cosinus_median",
      "D_cosinus_mean",
      "D_cosinus_std",
      "D_cosinus_percentile_90",
      "experiment"
    ),
    row.names = c(NA,-10L),
    class = c("tbl_df", "tbl",
              "data.frame")
  )


summarize_var <- function(df, D_type) {

var <- ensym(D_type)  

suffix <- "_mean_standard"

df %>% 
  summarize(!!paste0(var, suffix) := mean(!! var))

}

summarize_var(df, D_manhattan_scaled)
#> # A tibble: 1 x 1
#>   D_manhattan_scaled_mean_standard
#>                              <dbl>
#> 1                            0.123

reprex package(v0.2.1)于2018-11-28创建