亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將列折疊/連接/聚合到每個組中的單個逗號分隔字符串

將列折疊/連接/聚合到每個組中的單個逗號分隔字符串

慕后森 2019-05-25 16:20:07
將列折疊/連接/聚合到每個組中的單個逗號分隔字符串我想根據兩個分組變量聚合數據框中的一列,并用逗號分隔各個值。這是一些數據: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”和“B”是分組變量,“C”是我想要折疊成逗號分隔character字符串的變量。我試過了:library(plyr)ddply(data, .(A,B), summarise, test = list(C))    A B  test1 111 1  5, 72 111 2     63 222 1     94 222 2 8, 10但是當我試圖將測試列轉換為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)如何保留character格式并用逗號分隔?例如,第1行應該只是"5,7",而不是c(5,7)。
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

這里有一些選項使用toString,一個很好的實用程序函數,用逗號連接字符串。如果你不希望逗號,你可以使用paste()collapse參數來代替。

data.table

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

aggregate這不使用包:

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

sqldf

以下是group_concat使用sqldf包使用SQL函數的替代方法:

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

dplyrdplyr替代:

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

plyr

# plyrlibrary(plyr)ddply(data, .(A,B), summarize, C = toString(C))


查看完整回答
反對 回復 2019-05-25
?
Helenr

TA貢獻1780條經驗 獲得超4個贊

改變放置位置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

但請注意,每個項目實際上仍然是一個單獨的字符,而不是單個字符串。也就是說,這不是一個看起來像“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

基礎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"


查看完整回答
反對 回復 2019-05-25
?
森林海

TA貢獻2011條經驗 獲得超2個贊

這是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


查看完整回答
反對 回復 2019-05-25
  • 3 回答
  • 0 關注
  • 802 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號