我有一个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包
发布于 2015-12-09 15:27:16
您可以这样做,使用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当然,您可以根据自己的意愿在round调用中使用frac值。
https://stackoverflow.com/questions/34182210
复制相似问题