英文:
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 <- c("A", "A123", "A123", "B123", "B123", "B")
B <- c("NA", "as", "bp", "df", "kl", "c")
df <- data.frame(A, B)
and I would like to create a df in which the output would be
A <- c("A", "A123", "B123", "B")
C <- c("NA", "as;bp", "df;kl", "c")
df2 <- 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
使用tidyverse
和reframe
来为每个'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 %>%
reframe(C = if(all(is.na(B))) B else
str_c(B[complete.cases(B)], collapse = ";"), .by = "A")
-output
A C
1 A NA
2 A123 as;bp
3 B123 df;kl
4 B c
答案2
得分: 0
使用 aggregate
和 paste
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=";"), c("A", "C"))
A C
1 A NA
2 A123 as;bp
3 B c
4 B123 df;kl
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论