Mutate new columns and intercalate them with old ones.

huangapple go评论96阅读模式
英文:

Mutate new columns and intercalate them with old ones

问题

以下是您要翻译的内容:

"我想使用 across 创建新列,并使这些新列与旧列交错。在示例中,我手动重新排列列以显示所需的输出。但我希望能够自动执行此操作,就像我在 mutate 中尝试的方式一样,显然这种方式不起作用。

  1. library(dplyr)
  2. df <- tibble(a = 1:2, x_b = 1:2, x_c = 1:2)
  3. df |&gt;
  4. mutate(across(starts_with(&quot;x_&quot;),
  5. ~ .x * 2,
  6. .names = &quot;{sub(&#39;x_&#39;, &#39;y_&#39;, .col)}&quot;),
  7. .after = c(x_b, x_c)) |&gt;
  8. relocate(y_b, .after = x_b) |&gt;
  9. relocate(y_c, .after = x_c)
  10. #&gt; # A tibble: 2 &#215; 5
  11. #&gt; a x_b y_b x_c y_c
  12. #&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  13. #&gt; 1 1 1 2 1 2
  14. #&gt; 2 2 2 4 2 4

<sup>创建于2023年5月18日,使用 reprex v2.0.2</sup>"

英文:

I want to create new columns using across and that new columns being intercalated with the old ones. In the example I manually relocate the columns to show the desired output. But I would like to do this automatically, like my try inside mutate, which obviously does not work.

  1. library(dplyr)
  2. df &lt;- tibble(a = 1:2, x_b = 1:2, x_c = 1:2)
  3. df |&gt;
  4. mutate(across(starts_with(&quot;x_&quot;),
  5. ~ .x * 2,
  6. .names = &quot;{sub(&#39;x_&#39;, &#39;y_&#39;, .col)}&quot;),
  7. .after = c(x_b, x_c)) |&gt;
  8. relocate(y_b, .after = x_b) |&gt;
  9. relocate(y_c, .after = x_c)
  10. #&gt; # A tibble: 2 &#215; 5
  11. #&gt; a x_b y_b x_c y_c
  12. #&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  13. #&gt; 1 1 1 2 1 2
  14. #&gt; 2 2 2 4 2 4

<sup>Created on 2023-05-18 with reprex v2.0.2</sup>

答案1

得分: 5

We could create a tibble/data.frame, use .unpack option and rename the columns

  1. library(dplyr)
  2. library(stringr)
  3. df %>%
  4. mutate(across(starts_with("x_"),
  5. ~ data.frame(x = .x, y = .x * 2), .unpack = TRUE),
  6. .keep = 'unused') %>%
  7. rename_with(~ str_replace(.x, "x_(.)_(.)", "\_\"))

-output

  1. # A tibble: 2 × 5
  2. a x_b y_b x_c y_c
  3. <int> <int> <dbl> <int> <dbl>
  4. 1 1 1 2 1 2
  5. 2 2 2 4 2 4
英文:

We could create a tibble/data.frame, use .unpack option and rename the columns

  1. library(dplyr)
  2. library(stringr)
  3. df %&gt;%
  4. mutate(across(starts_with(&quot;x_&quot;),
  5. ~ data.frame(x = .x, y = .x * 2), .unpack = TRUE),
  6. .keep = &#39;unused&#39;) %&gt;%
  7. rename_with(~ str_replace(.x, &quot;x_(.)_(.)&quot;, &quot;\_\&quot;))

-output

  1. # A tibble: 2 &#215; 5
  2. a x_b y_b x_c y_c
  3. &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  4. 1 1 1 2 1 2
  5. 2 2 2 4 2 4
  6. </details>
  7. # 答案2
  8. **得分**: 3
  9. A dirty workaround that Im using regularly:
  10. library(tidyverse)
  11. prefix &lt;- c('x_', 'y_')
  12. suffix &lt;- c('b', 'c')
  13. col_order &lt;- paste0(prefix, rep(suffix, each = length(prefix)))
  14. df %&gt;%
  15. select(a, all_of(col_order))
  16. # A tibble: 2 x 5
  17. a x_b y_b x_c y_c
  18. &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  19. 1 1 1 2 1 2
  20. 2 2 2 4 2 4
  21. <details>
  22. <summary>英文:</summary>
  23. A dirty workaround that Im using regularly:
  24. library(tidyverse)
  25. prefix &lt;- c(&#39;x_&#39;, &#39;y_&#39;)
  26. suffix &lt;- c(&#39;b&#39;, &#39;c&#39;)
  27. col_order &lt;- paste0(prefix, rep(suffix, each = length(prefix)))
  28. df %&gt;%
  29. select(a, all_of(col_order))
  30. # A tibble: 2 x 5
  31. a x_b y_b x_c y_c
  32. &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  33. 1 1 1 2 1 2
  34. 2 2 2 4 2 4
  35. </details>

huangapple
  • 本文由 发表于 2023年5月18日 13:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定