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

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

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,
  &quot;1&quot;, 135L, 0.918367346938776,  &quot;q1&quot;,    &quot;Y&quot;,
  &quot;1&quot;, 135L, 0.918367346938776,  &quot;q2&quot;,    &quot;Y&quot;,
  &quot;3&quot;, 225L, 0.945378151260504,  &quot;q1&quot;,    &quot;Y&quot;,
  &quot;3&quot;, 225L, 0.945378151260504,  &quot;q3&quot;,    &quot;Y&quot;,
  &quot;5&quot;, 446L, 0.944915254237288,  &quot;q1&quot;,    &quot;Y&quot;,
  &quot;5&quot;, 446L, 0.944915254237288,  &quot;q4&quot;,    &quot;Y&quot;
)
#&gt; # A tibble: 6 x 5
#&gt;   id        n  prop name  value
#&gt;   &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;
#&gt; 1 1       135 0.918 q1    Y    
#&gt; 2 1       135 0.918 q2    Y    
#&gt; 3 3       225 0.945 q1    Y    
#&gt; 4 3       225 0.945 q3    Y    
#&gt; 5 5       446 0.945 q1    Y    
#&gt; 6 5       446 0.945 q4    Y

# desired structure
tibble::tribble(
  ~id,   ~n,             ~prop, ~name1, ~value1, ~name2, ~value2,
  &quot;1&quot;, 135L, 0.918367346938776,  &quot;q1&quot;,    &quot;Y&quot;,   &quot;q2&quot;,    &quot;Y&quot;,
  &quot;3&quot;, 225L, 0.945378151260504,  &quot;q1&quot;,    &quot;Y&quot;,   &quot;q3&quot;,    &quot;Y&quot;,
  &quot;5&quot;, 446L, 0.944915254237288,  &quot;q1&quot;,    &quot;Y&quot;,   &quot;q4&quot;,    &quot;Y&quot;
)
#&gt; # A tibble: 3 x 7
#&gt;   id        n  prop name1 value1 name2 value2
#&gt;   &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt;  &lt;chr&gt; &lt;chr&gt; 
#&gt; 1 1       135 0.918 q1    Y      q2    Y     
#&gt; 2 3       225 0.945 q1    Y      q3    Y     
#&gt; 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 |&gt;
  mutate(id_row = row_number(), .by = &quot;id&quot;) |&gt;
  pivot_wider(names_from = id_row, values_from = c(name, value))
# # A tibble: 3 &#215; 7
#   id        n  prop name_1 name_2 value_1 value_2
#   &lt;chr&gt; &lt;int&gt; &lt;dbl&gt; &lt;chr&gt;  &lt;chr&gt;  &lt;chr&gt;   &lt;chr&gt;  
# 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   

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:

确定