如何将特定列与数据框的每一列合并?

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

How to merge a specific column with each column of a dataframe?

问题

让我们假设我有这个数据框(我的原始数据框要大得多):

df = data.frame(ID = c(1,2,2,3,4,5,5),
                T = c(1,1,2,1,2,1,2),
                product_1 = c('A','A','B','C','A','C','D'),
                product_2 = c('C','A','B','A','B','E','C')
                )

我希望得到以下输出:

  ID product_1_T1 product_1_T2 product_2_T1 product_2_T2
1  1            A         <NA>            C         <NA>
2  2            A            B            A            B
3  3            C         <NA>            A         <NA>
4  4         <NA>            A         <NA>            B
5  5            C            D            E            C

如果你有任何其他问题,请随时提出。

英文:

Let's say I have this df (my original dataframe is so much bigger)

df = data.frame(ID = c(1,2,2,3,4,5,5),
                T = c(1,1,2,1,2,1,2),
                product_1 = c(&#39;A&#39;,&#39;A&#39;,&#39;B&#39;,&#39;C&#39;,&#39;A&#39;,&#39;C&#39;,&#39;D&#39;),
                product_2 = c(&#39;C&#39;,&#39;A&#39;,&#39;B&#39;,&#39;A&#39;,&#39;B&#39;,&#39;E&#39;,&#39;C&#39;),

                )

I wish to have the following output

  ID   product_1_T1 product_1_T2 product_2_T1 product_2_T2
1  1            A         &lt;NA&gt;            C         &lt;NA&gt;
2  2            A            B            A            B
3  3            C         &lt;NA&gt;            A         &lt;NA&gt;
4  4         &lt;NA&gt;            A         &lt;NA&gt;            B
5  5            C            D            E            C
&gt; 
````

</details>


# 答案1
**得分**: 1

```r
使用 `pivot_wider` 函数:
```r
library(tidyr)
pivot_wider(df, names_from = 'T', values_from = product_1:product_2, names_prefix = "T")
```

# 生成的数据表:
# 5 行 × 5 列
     ID product_1_T1 product_1_T2 product_2_T1 product_2_T2
  <dbl> <chr>        <chr>        <chr>        <chr>       
1     1 A            NA           C            NA          
2     2 A            B            A            B           
3     3 C            NA           A            NA          
4     4 NA           A            NA           B           
5     5 C            D            E            C           
```

<details>
<summary>英文:</summary>

With `pivot_wider`:
```r
library(tidyr)
pivot_wider(df, names_from = &#39;T&#39;, values_from = product_1:product_2, names_prefix = &quot;T&quot;)

# A tibble: 5 &#215; 5
     ID product_1_T1 product_1_T2 product_2_T1 product_2_T2
  &lt;dbl&gt; &lt;chr&gt;        &lt;chr&gt;        &lt;chr&gt;        &lt;chr&gt;       
1     1 A            NA           C            NA          
2     2 A            B            A            B           
3     3 C            NA           A            NA          
4     4 NA           A            NA           B           
5     5 C            D            E            C           
```

</details>



huangapple
  • 本文由 发表于 2023年7月31日 20:52:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803821.html
匿名

发表评论

匿名网友

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

确定