首页 > 解决方案 > Convert multiple rows into one row depending on unique values in another column

问题描述

I have two columns in a data frame. Based on the unique ids in column 1, I want to create a new column that has all the values from column 2 corresponding to that unique id in column1 separated by ,.

for example:

    col1<-c("x", "y", "x", "z", "x", "z")
    col2<-c("NY","UT","CA","SA", "SW", "GR")
    col3<-c(1, 5, 7, 4, 7, 2)
    df<-data.frame(col1, col2, col3,  stringsAsFactors=FALSE)

and I want

    col1<-c("x", "y", "z")
    col2<-c("NY CA SW", "UT","SA, GR")
    df2<-data.frame(col1, col2,  stringsAsFactors=FALSE)

标签: r

解决方案


library(dplyr); library(tidyr)

df %>% 
  group_by(col1) %>% 
  summarise(col2 = paste(col2, collapse=" "))

# # A tibble: 3 x 2
#   col1  col2    
#   <chr> <chr>   
# 1 x     NY CA SW
# 2 y     UT      
# 3 z     SA GR   

推荐阅读