根据R中另一列的重复值折叠行

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

Collapse rows based on another column duplicate value in R

问题

我有一个数据框:

A <- c("A", "A123", "A123", "B123", "B123", "B")
B <- c("NA", "as", "bp", "df", "kl", "c")

df <- data.frame(A, B)

我想创建一个数据框,其中输出如下:

A <- c("A", "A123", "B123", "B")
C <- c("NA", "as;bp", "df;kl", "c")
df2 <- data.frame(A, C)

这个新列基于如果在列A中有重复值,然后将列B中的值合并以创建新列C,所有在列A中对应单一/唯一值的列B中的其他唯一值将保留在列C中。

如果需要生成获取列C的代码,我将不提供翻译,但您可以参考以下示例代码来完成此任务:

library(dplyr)

df2 <- df %>%
  group_by(A) %>%
  summarise(C = paste(B, collapse = ";")) %>%
  ungroup()

希望对您有所帮助!

英文:

I have a df:

A &lt;- c(&quot;A&quot;, &quot;A123&quot;, &quot;A123&quot;, &quot;B123&quot;, &quot;B123&quot;, &quot;B&quot;)
B &lt;- c(&quot;NA&quot;, &quot;as&quot;, &quot;bp&quot;, &quot;df&quot;, &quot;kl&quot;, &quot;c&quot;)

df &lt;- data.frame(A, B) 

and I would like to create a df in which the output would be

A &lt;- c(&quot;A&quot;, &quot;A123&quot;, &quot;B123&quot;, &quot;B&quot;)
C &lt;- c(&quot;NA&quot;, &quot;as;bp&quot;, &quot;df;kl&quot;, &quot;c&quot;)
df2 &lt;- data.frame(A,C)

This new column is based on if there is a duplicate in column A, then combine the values in column B to make a new column, all other unique values in column B that correspond single/unique values in A would be carried over to column C.

Any help in generating a code where you get column C would be appreciated as I don't even know where to begin in coding for this.
thank you!

答案1

得分: 0

使用tidyversereframe来为每个'A'组合"粘贴"非缺失的'B'值 - 如果所有值都缺失,返回'B'列

library(dplyr)
library(stringr)
df %>%
  reframe(C = if(all(is.na(B))) B else 
    str_c(B[complete.cases(B)], collapse = ";"), .by = "A")
  • 输出
     A     C
1    A    NA
2 A123 as;bp
3 B123 df;kl
4    B     c
英文:

Use tidyverse with reframe to paste the non-missing 'B' values for each 'A' group - if all values are missing, return the B column

library(dplyr)
library(stringr)
df %&gt;%
   reframe(C = if(all(is.na(B))) B else 
     str_c(B[complete.cases(B)], collapse = &quot;;&quot;), .by = &quot;A&quot;)

-output

     A     C
1    A    NA
2 A123 as;bp
3 B123 df;kl
4    B     c

答案2

得分: 0

使用 aggregatepaste

setNames(aggregate(. ~ A, df, paste0, collapse=";"), c("A", "C"))
     A     C
1    A    NA
2 A123 as;bp
3    B     c
4 B123 df;kl
英文:

Using aggregate and paste

setNames(aggregate(. ~ A, df, paste0, collapse=&quot;;&quot;), c(&quot;A&quot;, &quot;C&quot;))
     A     C
1    A    NA
2 A123 as;bp
3    B     c
4 B123 df;kl

huangapple
  • 本文由 发表于 2023年2月18日 04:10:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488841.html
匿名

发表评论

匿名网友

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

确定