R:ddply 中元素的分数?
R: fraction of elements in ddply?
我有一个 3 列数据框。需要对它进行汇总,找出每个元素记录的分数w.r.t。它的子组中的记录总数(不是总数)。摘要对 ddply
很好用,但现在我被分数卡住了。
DataFrameCons = structure(list(Category = c("2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments"
), Manufacturer = c("Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Hasbro Inc", "Hasbro Inc", "Hasbro Inc",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "3M", "3M", "Laboratory Robaina Inc",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "Other", "Other"), Count = c(1,
1, 2, 1, 1, 1, 1, 6, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3, 1, 1, 1, 49, 206, 62, 4, 1, 1, 1, 1, 2, 1, 1, 244, 1, 16,
1, 1, 1, 2, 1, 2, 1, 16, 57, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 60, 109,
2, 7, 9, 6, 88, 1, 2, 1, 2, 3, 1, 3, 2, 5, 3, 2, 1, 1, 2, 4,
1, 4, 1, 1, 3, 1, 1, 1, 1, 1, 19, 1, 1, 2, 3, 1, 4, 1, 1, 1,
79, 1, 143, 1, 3, 1, 1, 1, 1, 1, 1, 2, 77, 1, 1, 1, 33, 1, 3,
1, 1, 63, 3, 3, 1, 5, 1, 10, 1, 663, 976, 1, 2, 1, 43, 1, 1,
4, 241, 1, 1, 1, 1, 1, 86, 1, 1, 3, 1, 244, 84, 1, 1, 1, 659,
1, 265, 13, 5, 1, 1, 169, 9, 38, 1, 1, 2014, 5, 3, 2, 1, 1, 2,
1)), .Names = c("Category", "Manufacturer", "Count"), class = "data.frame", row.names = c(NA,
215L))
counts_data_cons = ddply(DataFrameCons,.(Category,Manufacturer),summarise,records = sum(Count))#, manu = paste(unique(Manufacturer),collapse = "; "))
View(counts_data_cons)
输出:
Category Manufacturer records
1 2-Way Radios/Intercoms Hasbro Inc 59
2 2-Way Radios/Intercoms Motorola Inc 615
3 2-Way Radios/Intercoms Other 399
4 Acne Treatments 3M 6
5 Acne Treatments Laboratory Robaina Inc 1
6 Acne Treatments Other 3
7 Acne Treatments University Medical Pharmaceuticals 6039
期望的输出:
Category Manufacturer records frac
1 2-Way Radios/Intercoms Hasbro Inc 59 0.055
2 2-Way Radios/Intercoms Motorola Inc 615 0.573
3 2-Way Radios/Intercoms Other 399 0.372
4 Acne Treatments 3M 6 9.9e-4
5 Acne Treatments Laboratory Robaina Inc 1 1.7e-4
6 Acne Treatments Other 3 5.0e-4
7 Acne Treatments University Medical Pharmaceuticals 6039 0.998
其中 0.055 = 59 / (59 + 615 + 399)。谢谢!
如果可能,不要使用 dplyr
包
你可以这样使用 plyr
:
library(plyr)
ddply(counts_data_cons, .(Category), mutate, frac = records / sum(records))
# Category Manufacturer records frac
#1 2-Way Radios/Intercoms Hasbro Inc 59 0.0549860205
#2 2-Way Radios/Intercoms Motorola Inc 615 0.5731593663
#3 2-Way Radios/Intercoms Other 399 0.3718546132
#4 Acne Treatments 3M 6 0.0009918995
#5 Acne Treatments Laboratory Robaina Inc 1 0.0001653166
#6 Acne Treatments Other 3 0.0004959497
#7 Acne Treatments University Medical Pharmaceuticals 6039 0.9983468342
当然,您可以在 ddply
调用中 round
frac
值。
我有一个 3 列数据框。需要对它进行汇总,找出每个元素记录的分数w.r.t。它的子组中的记录总数(不是总数)。摘要对 ddply
很好用,但现在我被分数卡住了。
DataFrameCons = structure(list(Category = c("2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"2-Way Radios/Intercoms", "2-Way Radios/Intercoms", "2-Way Radios/Intercoms",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments",
"Acne Treatments", "Acne Treatments", "Acne Treatments", "Acne Treatments"
), Manufacturer = c("Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Motorola Inc", "Motorola Inc",
"Motorola Inc", "Motorola Inc", "Hasbro Inc", "Hasbro Inc", "Hasbro Inc",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "Other", "Other",
"Other", "Other", "Other", "Other", "Other", "3M", "3M", "Laboratory Robaina Inc",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "University Medical Pharmaceuticals",
"University Medical Pharmaceuticals", "Other", "Other"), Count = c(1,
1, 2, 1, 1, 1, 1, 6, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1,
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3, 1, 1, 1, 49, 206, 62, 4, 1, 1, 1, 1, 2, 1, 1, 244, 1, 16,
1, 1, 1, 2, 1, 2, 1, 16, 57, 1, 1, 1, 1, 6, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 60, 109,
2, 7, 9, 6, 88, 1, 2, 1, 2, 3, 1, 3, 2, 5, 3, 2, 1, 1, 2, 4,
1, 4, 1, 1, 3, 1, 1, 1, 1, 1, 19, 1, 1, 2, 3, 1, 4, 1, 1, 1,
79, 1, 143, 1, 3, 1, 1, 1, 1, 1, 1, 2, 77, 1, 1, 1, 33, 1, 3,
1, 1, 63, 3, 3, 1, 5, 1, 10, 1, 663, 976, 1, 2, 1, 43, 1, 1,
4, 241, 1, 1, 1, 1, 1, 86, 1, 1, 3, 1, 244, 84, 1, 1, 1, 659,
1, 265, 13, 5, 1, 1, 169, 9, 38, 1, 1, 2014, 5, 3, 2, 1, 1, 2,
1)), .Names = c("Category", "Manufacturer", "Count"), class = "data.frame", row.names = c(NA,
215L))
counts_data_cons = ddply(DataFrameCons,.(Category,Manufacturer),summarise,records = sum(Count))#, manu = paste(unique(Manufacturer),collapse = "; "))
View(counts_data_cons)
输出:
Category Manufacturer records
1 2-Way Radios/Intercoms Hasbro Inc 59
2 2-Way Radios/Intercoms Motorola Inc 615
3 2-Way Radios/Intercoms Other 399
4 Acne Treatments 3M 6
5 Acne Treatments Laboratory Robaina Inc 1
6 Acne Treatments Other 3
7 Acne Treatments University Medical Pharmaceuticals 6039
期望的输出:
Category Manufacturer records frac
1 2-Way Radios/Intercoms Hasbro Inc 59 0.055
2 2-Way Radios/Intercoms Motorola Inc 615 0.573
3 2-Way Radios/Intercoms Other 399 0.372
4 Acne Treatments 3M 6 9.9e-4
5 Acne Treatments Laboratory Robaina Inc 1 1.7e-4
6 Acne Treatments Other 3 5.0e-4
7 Acne Treatments University Medical Pharmaceuticals 6039 0.998
其中 0.055 = 59 / (59 + 615 + 399)。谢谢!
如果可能,不要使用 dplyr
包
你可以这样使用 plyr
:
library(plyr)
ddply(counts_data_cons, .(Category), mutate, frac = records / sum(records))
# Category Manufacturer records frac
#1 2-Way Radios/Intercoms Hasbro Inc 59 0.0549860205
#2 2-Way Radios/Intercoms Motorola Inc 615 0.5731593663
#3 2-Way Radios/Intercoms Other 399 0.3718546132
#4 Acne Treatments 3M 6 0.0009918995
#5 Acne Treatments Laboratory Robaina Inc 1 0.0001653166
#6 Acne Treatments Other 3 0.0004959497
#7 Acne Treatments University Medical Pharmaceuticals 6039 0.9983468342
当然,您可以在 ddply
调用中 round
frac
值。