R:ddply中的元素分数?

时间:2015-12-09 15:09:55

标签: r plyr

我有一个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

1 个答案:

答案 0 :(得分:2)

你可以使用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 ddply groupBy个值。