尝试
dplyr::group_by()
df <- data.frame(
col1 = c("A", "A", "B", "C", "D", "C"),
col2 = c("V1", "V2", "V3", "V4", "V5", "V6")
)
library(dplyr)
result <- df %>% group_by(col1) %>% summarize(col2 = paste(col2, collapse = ",")) %>% ungroup()
现在的结果就是你所期望的
# A tibble: 4 × 2
col1 col2
<chr> <chr>
1 A V1,V2
2 B V3
3 C V4,V6
4 D V5