使用tostring聚合字符串并在r中对它们进行计数 [英] Aggregating strings using tostring and counting them in r
本文介绍了使用tostring聚合字符串并在r中对它们进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
应用dplyr代码后,我获得了以下数据框
I have following dataframe got after applying dplyr code
Final_df<- df %>%
group_by(clientID,month) %>%
summarise(test=toString(Sector)) %>%
as.data.frame()
哪个给我以下输出
ClientID month test
ASD Sep Auto,Auto,Finance
DFG Oct Finance,Auto,Oil
我想同时计数扇区
ClientID month test
ASD Sep Auto:2,Finance:1
DFG Oct Finance:1,Auto:1,Oil:1
如何使用dplyr实现它?
How can I achieve it with dplyr?
推荐答案
我们可以尝试
df %>%
group_by(client_id, month, Sector) %>%
tally() %>%
group_by(client_id, month) %>%
summarise(test = toString(paste(Sector, n, sep=":")))
或使用 data.table
library(data.table)
setDT(df)[, .N, .(ClientID, month, Sector)
][, .(test = toString(paste(Sector, N, sep=":"))) , .(ClientID, month)]
如果我们需要 base R
aggregate(newCol~ClientID + month, transform(aggregate(n~.,
transform(df, n = 1), sum), newCol = paste(Sector, n, sep=":")), toString)
数据
df <- data.frame(ClientID = rep(c("ASD.", "DFG."), each = 5),
month = rep(c("Sep", "Oct" ) , c(3,2)),
Sector = c("Auto", "Auto", "Finance", "Finance", "Finance",
"Auto", "Finance", "Finance", "Oil", "Oil"),
stringsAsFactors=FALSE)
这篇关于使用tostring聚合字符串并在r中对它们进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文