将2行合并为一行,添加额外列。

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

Collapse 2 rows into a single row by adding extra columns

问题

I currently have data in a format that has exactly 2 rows per id. I would like to collapse those rows into a single row like below. I've tried using pivot_wider(), but that gives me too many columns. I only want two extra columns. How can I achieve this?

  1. # current structure
  2. tibble::tribble(
  3. ~id, ~n, ~prop, ~name, ~value,
  4. "1", 135L, 0.918367346938776, "q1", "Y",
  5. "1", 135L, 0.918367346938776, "q2", "Y",
  6. "3", 225L, 0.945378151260504, "q1", "Y",
  7. "3", 225L, 0.945378151260504, "q3", "Y",
  8. "5", 446L, 0.944915254237288, "q1", "Y",
  9. "5", 446L, 0.944915254237288, "q4", "Y"
  10. )
  11. #> # A tibble: 6 x 5
  12. #> id n prop name value
  13. #> <chr> <int> <dbl> <chr> <chr>
  14. #> 1 1 135 0.918 q1 Y
  15. #> 2 1 135 0.918 q2 Y
  16. #> 3 3 225 0.945 q1 Y
  17. #> 4 3 225 0.945 q3 Y
  18. #> 5 5 446 0.945 q1 Y
  19. #> 6 5 446 0.945 q4 Y
  20. # desired structure
  21. tibble::tribble(
  22. ~id, ~n, ~prop, ~name1, ~value1, ~name2, ~value2,
  23. "1", 135L, 0.918367346938776, "q1", "Y", "q2", "Y",
  24. "3", 225L, 0.945378151260504, "q1", "Y", "q3", "Y",
  25. "5", 446L, 0.944915254237288, "q1", "Y", "q4", "Y"
  26. )
  27. #> # A tibble: 3 x 7
  28. #> id n prop name1 value1 name2 value2
  29. #> <chr> <int> <dbl> <chr> <chr> <chr> <chr>
  30. #> 1 1 135 0.918 q1 Y q2 Y
  31. #> 2 3 225 0.945 q1 Y q3 Y
  32. #> 3 5 446 0.945 q1 Y q4 Y

Created on 2023-06-08 with reprex v2.0.2

英文:

I currently have data in a format that has exactly 2 rows per id. I would like to collapse those rows into a single row like below. I've tried using pivot_wider(), but that gives me too many columns. I only want two extra columns. How can I achieve this?

  1. # current structure
  2. tibble::tribble(
  3. ~id, ~n, ~prop, ~name, ~value,
  4. &quot;1&quot;, 135L, 0.918367346938776, &quot;q1&quot;, &quot;Y&quot;,
  5. &quot;1&quot;, 135L, 0.918367346938776, &quot;q2&quot;, &quot;Y&quot;,
  6. &quot;3&quot;, 225L, 0.945378151260504, &quot;q1&quot;, &quot;Y&quot;,
  7. &quot;3&quot;, 225L, 0.945378151260504, &quot;q3&quot;, &quot;Y&quot;,
  8. &quot;5&quot;, 446L, 0.944915254237288, &quot;q1&quot;, &quot;Y&quot;,
  9. &quot;5&quot;, 446L, 0.944915254237288, &quot;q4&quot;, &quot;Y&quot;
  10. )
  11. #&gt; # A tibble: 6 x 5
  12. #&gt; id n prop name value
  13. #&gt; &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;
  14. #&gt; 1 1 135 0.918 q1 Y
  15. #&gt; 2 1 135 0.918 q2 Y
  16. #&gt; 3 3 225 0.945 q1 Y
  17. #&gt; 4 3 225 0.945 q3 Y
  18. #&gt; 5 5 446 0.945 q1 Y
  19. #&gt; 6 5 446 0.945 q4 Y
  20. # desired structure
  21. tibble::tribble(
  22. ~id, ~n, ~prop, ~name1, ~value1, ~name2, ~value2,
  23. &quot;1&quot;, 135L, 0.918367346938776, &quot;q1&quot;, &quot;Y&quot;, &quot;q2&quot;, &quot;Y&quot;,
  24. &quot;3&quot;, 225L, 0.945378151260504, &quot;q1&quot;, &quot;Y&quot;, &quot;q3&quot;, &quot;Y&quot;,
  25. &quot;5&quot;, 446L, 0.944915254237288, &quot;q1&quot;, &quot;Y&quot;, &quot;q4&quot;, &quot;Y&quot;
  26. )
  27. #&gt; # A tibble: 3 x 7
  28. #&gt; id n prop name1 value1 name2 value2
  29. #&gt; &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  30. #&gt; 1 1 135 0.918 q1 Y q2 Y
  31. #&gt; 2 3 225 0.945 q1 Y q3 Y
  32. #&gt; 3 5 446 0.945 q1 Y q4 Y

<sup>Created on 2023-06-08 with reprex v2.0.2</sup>

答案1

得分: 1

这是您提供的代码的翻译部分:

  1. library(dplyr)
  2. library(tdiyr)
  3. your_data |>
  4. mutate(id_row = row_number(), .by = "id") |>
  5. pivot_wider(names_from = id_row, values_from = c(name, value))
  6. # # A tibble: 3 × 7
  7. # id n prop name_1 name_2 value_1 value_2
  8. # <chr> <int> <dbl> <chr> <chr> <chr> <chr>
  9. # 1 1 135 0.918 q1 q2 Y Y
  10. # 2 3 225 0.945 q1 q3 Y Y
  11. # 3 5 446 0.945 q1 q4 Y Y

请注意,我只提供了代码的翻译部分,没有其他内容。

英文:
  1. library(dplyr)
  2. library(tdiyr)
  3. your_data |&gt;
  4. mutate(id_row = row_number(), .by = &quot;id&quot;) |&gt;
  5. pivot_wider(names_from = id_row, values_from = c(name, value))
  6. # # A tibble: 3 &#215; 7
  7. # id n prop name_1 name_2 value_1 value_2
  8. # &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
  9. # 1 1 135 0.918 q1 q2 Y Y
  10. # 2 3 225 0.945 q1 q3 Y Y
  11. # 3 5 446 0.945 q1 q4 Y Y

huangapple
  • 本文由 发表于 2023年6月9日 01:47:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434478.html
匿名

发表评论

匿名网友

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

确定