Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

r - Use mutate and dynamically named variables in dplyr

I would like to apply a function that selects the best transformation of certain variables in a data frame, and then adds new columns to the data frame with the transformed data. I can currently get the transformation to run as follows. However, this rewrites the existing data, instead of adding new, transformed variables. I have seen the other stackoverflow posts about dynamically-added variables but can't quite seem to get it to work. Here is what I have:

df <- data.frame(study_id = c(1:10),
                 v1 = (sample(1:100, 10)),
                 v2 = (sample(1:100, 10)),
                 v3 = (sample(1:100, 10)),
                 v4 = (sample(1:100, 10)))

require(bestNormalize)

transformed <- function(x) {
  bn <- bestNormalize(x)
  return(bn$x.t)
}

df <- df %>%
  mutate(across(c(2,4:5), transformed))

Current output:

   study_id           v1 v2         v3          v4
1         1 -0.001846842 43  0.6559159  0.37893888
2         2 -2.416625847 81 -1.2998111 -0.64356058
3         3  1.012132345 95 -1.5086228 -0.48845289
4         4  0.798561562  2  0.8301299  0.30168982
5         5 -0.257460026 35  0.1322051  0.78737617
6         6 -0.179681789 42 -1.1352463 -2.42438347
7         7  0.378206706 22 -0.3635088  0.79583687
8         8  0.909304988 70  1.0748401  0.63712357
9         9  0.325879668 32  0.9041796 -0.09711216
10       10 -0.568470765  7  0.7099185  0.75254380

Desired output:

   study_id  v1 v2 v3  v4 v1_transformed v3_transformed v4_transformed
1         1  72  7 87 100              4              3              2
2         2  57 78 64  69             10              8              6
3         3  35 65 83  96              3              5              4
4         4  24 58 94  53              6             10             10
5         5 100 62 82  63             -1              7              3
6         6  47 55  4  50              8              4              1
7         7  83 97 35  41              7              2             -1
8         8  78 86 22  73              1             -1              9
9         9  11 39 93  68              2              0              7
10       10  36 49  8  72              0              1              0

Many thanks in advance.

question from:https://stackoverflow.com/questions/66067268/use-mutate-and-dynamically-named-variables-in-dplyr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the .names= argument of across:

df %>%
  mutate(across(c(2,4:5), transformed, .names = "{.col}_transformed"))

giving:

   study_id v1 v2 v3 v4 v1_transformed v3_transformed v4_transformed
1         1 50 72 12  7      0.3850197     -0.7916019     -1.9775107
2         2 53 82 61 42      0.4425318      0.6132865      0.6790496
3         3  3 12 90 20     -2.3661268      0.9496526     -0.4232995
4         4 20 84 37 21     -0.5190229      0.1809655     -0.3508475
5         5 55 54  4 23      0.4790925     -1.7301008     -0.2157362
6         6 61 96 85 74      0.5812924      0.9002185      1.5209888
7         7 52 94 22 38      0.4237308     -0.2683955      0.5302984
8         8 72 41 57 35      0.7449435      0.5546340      0.4080778
9         9 13 67  6 45     -0.9434502     -1.3866702      0.7815968
10       10 74 48 93 14      0.7719892      0.9780114     -0.9526174

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...