R:汇总字符串 [英] R: Aggregate character strings
本文介绍了R:汇总字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框 ModelDF
,其中包含具有数字以及字符值的列,例如:
I have a data frame ModelDF
having columns with numeric as well as character values like:
Quantity Type Mode Company
1 Shoe hello Nike
1 Shoe hello Nike
2 Jeans hello Levis
3 Shoe hello Nike
1 Jeans hello Levis
1 Shoe hello Adidas
2 Jeans hello Spykar
1 Shoe ahola Nike
1 Jeans ahola Levis
我必须以这种形式汇总
Quantity Type Mode Company
5 Shoe hello Nike
3 jeans hello Levis
1 Shoe hello adidas
2 jeans hello Spykar
1 Shoe ahola Nike
1 jeans ahola Levis
i.e。如果所有其他列都相同,我必须对数量进行汇总和求和。
i.e. I have to aggregate and sum Quantity if all other columns are same.
我尝试使用 aggregate
进行尝试,但
I have tried it using aggregate
but as it doesn't work on character values it is giving me wrong results.
我有什么选择?
谢谢
What are my options? Thanks
推荐答案
aggregate(Quantity ~ Type + Mode + Company, df, sum)
# Type Mode Company Quantity
#1 Shoe hello Adidas 1
#2 Jeans ahola Levis 1
#3 Jeans hello Levis 3
#4 Shoe ahola Nike 1
#5 Shoe hello Nike 5
#6 Jeans hello Spykar 2
您也可以尝试使用 data.table
选项:
You can also try the data.table
option:
setDT(df)[, .(Sum.Quantity = sum(Quantity)), by = list(Type, Mode, Company)]
# Type Mode Company Sum.Quantity
#1: Shoe hello Nike 5
#2: Jeans hello Levis 3
#3: Shoe hello Adidas 1
#4: Jeans hello Spykar 2
#5: Shoe ahola Nike 1
#6: Jeans ahola Levis 1
与 dplyr $ c类似$ c>
df %>%
group_by(Type, Mode, Company) %>%
summarise(sum(Quantity))
数据
dput(df)
structure(list(Quantity = c(1L, 1L, 2L, 3L, 1L, 1L, 2L, 1L, 1L
), Type = structure(c(2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Jeans",
"Shoe"), class = "factor"), Mode = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L), .Label = c("ahola", "hello"), class = "factor"),
Company = structure(c(3L, 3L, 2L, 3L, 2L, 1L, 4L, 3L, 2L), .Label = c("Adidas",
"Levis", "Nike", "Spykar"), class = "factor")), .Names = c("Quantity",
"Type", "Mode", "Company"), class = "data.frame", row.names = c(NA,
-9L))
这篇关于R:汇总字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文