Collapse / concatenate / aggregate a column to a single comma separated string within each group
Asked Answered
H

6

113

I want to aggregate one column in a data frame according to two grouping variables, and separate the individual values by a comma.

Here is some data:

data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = rep(1:2, 3), C = c(5:10))
data
#     A B  C
# 1 111 1  5
# 2 111 2  6
# 3 111 1  7
# 4 222 2  8
# 5 222 1  9
# 6 222 2 10    

"A" and "B" are grouping variables, and "C" is the variable that I want to collapse into a comma separated character string. I have tried:

library(plyr)
ddply(data, .(A,B), summarise, test = list(C))

    A B  test
1 111 1  5, 7
2 111 2     6
3 222 1     9
4 222 2 8, 10

but when I tried to convert test column to character it becomes like this:

ddply(data, .(A,B), summarise, test = as.character(list(C)))
#     A B     test
# 1 111 1  c(5, 7)
# 2 111 2        6
# 3 222 1        9
# 4 222 2 c(8, 10)

How can I keep the character format and separate them by a comma? For example, row 1 should be only "5,7", and not as c(5,7).

Hispania answered 10/4, 2013 at 18:47 Comment(0)
U
123

Here are some options using toString, a function that concatenates a vector of strings using comma and space to separate components. If you don't want commas, you can use paste() with the collapse argument instead.

data.table

# alternative using data.table
library(data.table)
as.data.table(data)[, toString(C), by = list(A, B)]

aggregate This uses no packages:

# alternative using aggregate from the stats package in the core of R
aggregate(C ~., data, toString)

sqldf

And here is an alternative using the SQL function group_concat using the sqldf package :

library(sqldf)
sqldf("select A, B, group_concat(C) C from data group by A, B", method = "raw")

dplyr A dplyr alternative:

library(dplyr)
data %>%
  group_by(A, B) %>%
  summarise(test = toString(C)) %>%
  ungroup()

or with more recent versions of dplyr

data %>%  summarise(test = toString(C), .by = c(A, B))

plyr

# plyr
library(plyr)
ddply(data, .(A,B), summarize, C = toString(C))
Unrealizable answered 10/4, 2013 at 19:53 Comment(1)
To keep unique values only: as.data.table(data)[, toString(unique(C)), by = list(A, B)]Scission
N
40

Here's the stringr/tidyverse solution:

library(tidyverse)
library(stringr)

data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = rep(1:2, 3), C = c(5:10))


data %>%
 group_by(A, B) %>%
 summarize(text = str_c(C, collapse = ", "))

# A tibble: 4 x 3
# Groups:   A [2]
      A     B text 
  <dbl> <int> <chr>
1   111     1 5, 7 
2   111     2 6    
3   222     1 9    
4   222     2 8, 10
Nickelson answered 11/4, 2019 at 12:45 Comment(1)
One can also substitute stringr::str_c for paste from base R.Dol
S
16

Change where you put as.character:

> out <- ddply(data, .(A, B), summarise, test = list(as.character(C)))
> str(out)
'data.frame':   4 obs. of  3 variables:
 $ A   : num  111 111 222 222
 $ B   : int  1 2 1 2
 $ test:List of 4
  ..$ : chr  "5" "7"
  ..$ : chr "6"
  ..$ : chr "9"
  ..$ : chr  "8" "10"
> out
    A B  test
1 111 1  5, 7
2 111 2     6
3 222 1     9
4 222 2 8, 10

Note in this case that each item is still actually a separate character, not a single character string. That is, this is not an actual string that looks like "5, 7", but rather, two characters, "5" and "7", which R displays with a comma between them.

Compare with the following:

> out2 <- ddply(data, .(A, B), summarise, test = paste(C, collapse = ", "))
> str(out2)
'data.frame':   4 obs. of  3 variables:
 $ A   : num  111 111 222 222
 $ B   : int  1 2 1 2
 $ test: chr  "5, 7" "6" "9" "8, 10"
> out
    A B  test
1 111 1  5, 7
2 111 2     6
3 222 1     9
4 222 2 8, 10

The comparable solution in base R is, of course, aggregate:

> A1 <- aggregate(C ~ A + B, data, function(x) c(as.character(x)))
> str(A1)
'data.frame':   4 obs. of  3 variables:
 $ A: num  111 222 111 222
 $ B: int  1 1 2 2
 $ C:List of 4
  ..$ 0: chr  "5" "7"
  ..$ 1: chr "9"
  ..$ 2: chr "6"
  ..$ 3: chr  "8" "10"
> A2 <- aggregate(C ~ A + B, data, paste, collapse = ", ")
> str(A2)
'data.frame':   4 obs. of  3 variables:
 $ A: num  111 222 111 222
 $ B: int  1 1 2 2
 $ C: chr  "5, 7" "9" "6" "8, 10"
Susannsusanna answered 10/4, 2013 at 18:54 Comment(0)
S
4

There is a small improvement here to avoid duplicates

# 1. Original data set
data <- data.frame(
  A = c(rep(111, 3), rep(222, 3)), 
  B = rep(1:2, 3), 
  C = c(5:10))

# 2. Add duplicate row
data <- rbind(data, data.table(
  A = 111, B = 1, C = 5
))

# 3. Solution with duplicates
data %>%
  group_by(A, B) %>%
  summarise(test = toString(C)) %>%
  ungroup()

#      A     B test   
#   <dbl> <dbl> <chr>  
# 1   111     1 5, 7, 5
# 2   111     2 6      
# 3   222     1 9      
# 4   222     2 8, 10

# 4. Solution without duplicates
data %>%
  select(A, B, C) %>% unique() %>% 
  group_by(A, B) %>%
  summarise(test = toString(C)) %>%
  ungroup()

#    A     B test 
#   <dbl> <dbl> <chr>
# 1   111     1 5, 7 
# 2   111     2 6    
# 3   222     1 9    
# 4   222     2 8, 10

Hope it can be useful.

Staysail answered 15/4, 2020 at 11:37 Comment(0)
D
2

Using collap from collapse

library(collapse)
collap(data, ~ A + B, toString)
    A B     C
1 111 1  5, 7
2 111 2     6
3 222 1     9
4 222 2 8, 10

data

data <- data.frame(A = c(rep(111, 3), rep(222, 3)), B = rep(1:2, 3), C = c(5:10))
Depicture answered 22/9, 2021 at 3:42 Comment(0)
I
2

An updated dplyr 1.1.0 solution with inline grouping with .by:

data %>% 
  summarise(test = toString(C), .by = c(A, B))

    A B  test
1 111 1  5, 7
2 111 2     6
3 222 2 8, 10
4 222 1     9

Benchmark:

benchmark <-
  bench::mark(
  data.table = as.data.table(data)[, toString(C), by = list(A, B)],
  aggregate = aggregate(C ~., data, toString),
  sqldf = sqldf("select A, B, group_concat(C) C from data group by A, B", method = "raw"),
  dplyr1.0.0 = data %>%
    group_by(A, B) %>%
    summarise(test = toString(C)) %>%
    ungroup(),
  dplyr1.1.0 = summarise(data, test = toString(C), .by = c(A, B)),
  collapse = collap(data, ~ A + B, toString),
  min_iterations = 30,
  check = FALSE
)

plot(benchmark)

enter image description here

Inviolable answered 31/1, 2023 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.