将列折叠/连接/聚合到每个组中的单个逗号分隔字符串

lin*_*inp 53 aggregate r r-faq

我想根据两个分组变量聚合数据框中的一列,并用逗号分隔各个值.

这是一些数据:

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    
Run Code Online (Sandbox Code Playgroud)

"A"和"B"是分组变量,"C"是我想要折叠成逗号分隔character字符串的变量.我试过了:

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
Run Code Online (Sandbox Code Playgroud)

但是当我试图将测试列转换为character它时,它变成了这样:

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)
Run Code Online (Sandbox Code Playgroud)

如何保留character格式并用逗号分隔?例如,第1行应该只是"5,7",而不是c(5,7).

G. *_*eck 60

plyr尝试使用toString:

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

以下是一些其他替代方案paste():

data.table

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

aggregate这不使用包:

library(sqldf)
sqldf("select A, B, group_concat(C) C from data group by A, B", method = "raw")
Run Code Online (Sandbox Code Playgroud)

sqldf

以下是collapse使用sqldf包使用SQL函数的替代方法:

library(dplyr)
data %>%
  group_by(A, B) %>%
  summarise(test = toString(C)) %>%
  ungroup()
Run Code Online (Sandbox Code Playgroud)

dplyrgroup_concat替代:

# plyr
library(plyr)
ddply(data, .(A,B), summarize, C = toString(C))
Run Code Online (Sandbox Code Playgroud)

  • 仅保留唯一值: as.data.table(data)[, toString(unique(C)), by = list(A, B)] (3认同)

A5C*_*2T1 13

改变放置位置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
Run Code Online (Sandbox Code Playgroud)

但请注意,每个项目实际上仍然是一个单独的字符,而不是单个字符串.也就是说,这不是一个看起来像"5,7"的实际字符串,而是两个字符"5"和"7",R在它们之间用逗号显示.

与以下内容比较:

> 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
Run Code Online (Sandbox Code Playgroud)

基础R中的可比解决方案当然是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"
Run Code Online (Sandbox Code Playgroud)


Ben*_*n G 7

这是stringr/ tidyverse解决方案:

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 test 
  <dbl> <int> <chr>
1   111     1 5, 7 
2   111     2 6    
3   222     1 9    
4   222     2 8, 10
Run Code Online (Sandbox Code Playgroud)

  • 也可以用 `stringr::str_c` 代替来自基础 R 的 `paste`。 (2认同)