英文:
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?
# current structure
tibble::tribble(
~id, ~n, ~prop, ~name, ~value,
"1", 135L, 0.918367346938776, "q1", "Y",
"1", 135L, 0.918367346938776, "q2", "Y",
"3", 225L, 0.945378151260504, "q1", "Y",
"3", 225L, 0.945378151260504, "q3", "Y",
"5", 446L, 0.944915254237288, "q1", "Y",
"5", 446L, 0.944915254237288, "q4", "Y"
)
#> # A tibble: 6 x 5
#> id n prop name value
#> <chr> <int> <dbl> <chr> <chr>
#> 1 1 135 0.918 q1 Y
#> 2 1 135 0.918 q2 Y
#> 3 3 225 0.945 q1 Y
#> 4 3 225 0.945 q3 Y
#> 5 5 446 0.945 q1 Y
#> 6 5 446 0.945 q4 Y
# desired structure
tibble::tribble(
~id, ~n, ~prop, ~name1, ~value1, ~name2, ~value2,
"1", 135L, 0.918367346938776, "q1", "Y", "q2", "Y",
"3", 225L, 0.945378151260504, "q1", "Y", "q3", "Y",
"5", 446L, 0.944915254237288, "q1", "Y", "q4", "Y"
)
#> # A tibble: 3 x 7
#> id n prop name1 value1 name2 value2
#> <chr> <int> <dbl> <chr> <chr> <chr> <chr>
#> 1 1 135 0.918 q1 Y q2 Y
#> 2 3 225 0.945 q1 Y q3 Y
#> 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?
# current structure
tibble::tribble(
~id, ~n, ~prop, ~name, ~value,
"1", 135L, 0.918367346938776, "q1", "Y",
"1", 135L, 0.918367346938776, "q2", "Y",
"3", 225L, 0.945378151260504, "q1", "Y",
"3", 225L, 0.945378151260504, "q3", "Y",
"5", 446L, 0.944915254237288, "q1", "Y",
"5", 446L, 0.944915254237288, "q4", "Y"
)
#> # A tibble: 6 x 5
#> id n prop name value
#> <chr> <int> <dbl> <chr> <chr>
#> 1 1 135 0.918 q1 Y
#> 2 1 135 0.918 q2 Y
#> 3 3 225 0.945 q1 Y
#> 4 3 225 0.945 q3 Y
#> 5 5 446 0.945 q1 Y
#> 6 5 446 0.945 q4 Y
# desired structure
tibble::tribble(
~id, ~n, ~prop, ~name1, ~value1, ~name2, ~value2,
"1", 135L, 0.918367346938776, "q1", "Y", "q2", "Y",
"3", 225L, 0.945378151260504, "q1", "Y", "q3", "Y",
"5", 446L, 0.944915254237288, "q1", "Y", "q4", "Y"
)
#> # A tibble: 3 x 7
#> id n prop name1 value1 name2 value2
#> <chr> <int> <dbl> <chr> <chr> <chr> <chr>
#> 1 1 135 0.918 q1 Y q2 Y
#> 2 3 225 0.945 q1 Y q3 Y
#> 3 5 446 0.945 q1 Y q4 Y
<sup>Created on 2023-06-08 with reprex v2.0.2</sup>
答案1
得分: 1
这是您提供的代码的翻译部分:
library(dplyr)
library(tdiyr)
your_data |>
mutate(id_row = row_number(), .by = "id") |>
pivot_wider(names_from = id_row, values_from = c(name, value))
# # A tibble: 3 × 7
# id n prop name_1 name_2 value_1 value_2
# <chr> <int> <dbl> <chr> <chr> <chr> <chr>
# 1 1 135 0.918 q1 q2 Y Y
# 2 3 225 0.945 q1 q3 Y Y
# 3 5 446 0.945 q1 q4 Y Y
请注意,我只提供了代码的翻译部分,没有其他内容。
英文:
library(dplyr)
library(tdiyr)
your_data |>
mutate(id_row = row_number(), .by = "id") |>
pivot_wider(names_from = id_row, values_from = c(name, value))
# # A tibble: 3 × 7
# id n prop name_1 name_2 value_1 value_2
# <chr> <int> <dbl> <chr> <chr> <chr> <chr>
# 1 1 135 0.918 q1 q2 Y Y
# 2 3 225 0.945 q1 q3 Y Y
# 3 5 446 0.945 q1 q4 Y Y
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论