Concatenating all rows within a group using dplyr

倾然丶 夕夏残阳落幕 提交于 2019-12-06 01:32:03

You were kind of close!

library(tidyr)
library(dplyr)

data <- read_csv('data.csv')
byHand <- group_by(data, hand_id) %>%
    summarise(combo_1 = paste(card_id, collapse = "-"), 
              combo_2 = paste(card_name, collapse = "-"),
              combo_3 = paste(card_class, collapse = "-"))

or using summarise_each:

 byHand <- group_by(data, hand_id) %>%
        summarise_each(funs(paste(., collapse = "-")))

Here is another option using data.table

library(data.table)
setDT(data)[, lapply(.SD, paste, collapse="-") , by = hand_id]
#     hand_id card_id card_name       card_class
#1:       A   1-2-3     p-q-r alpha-beta-theta
#2:       B   2-3-4     q-r-s beta-theta-gamma
#3:       C     1-2       p-q       alpha-beta

Not very familiar with dplyr... so here's my attempt without dplyr

df <- read_csv('data.csv')

res <- lapply(split(df, df$hand_id),function(x){
    sL <- apply(x[,-1], 2, function(y) paste(y, collapse = "-"))
    d <- data.frame(x$hand_id[1], rbind(sL))
    names(d) <- c("hand_id", "combo_1", "combo_2", "combo_3")
    return(d)
})
res <- do.call("rbind",res)
rownames(res) <- NULL

Here's the output:

##   hand_id combo_1 combo_2          combo_3
## 1       A   1-2-3   p-q-r alpha-beta-theta
## 2       B   2-3-4   q-r-s beta-theta-gamma
## 3       C     1-2     p-q       alpha-beta
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!