Alexey Ferapontov
Alexey Ferapontov

Reputation: 5169

R: fraction of elements in ddply?

I have a 3 column data frame. Need to summarize it and find a fraction of each element record w.r.t. total number of records in it's subgroup (not total overall). Summary works fine with ddply, but now I'm stuck with the fractions.

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)

Output:

                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

Desired output:

                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

where 0.055 = 59 / (59 + 615 + 399). Thanks! If possible, not using dplyr package

Upvotes: 0

Views: 100

Answers (1)

talat
talat

Reputation: 70266

You can do it like this using 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

Of course you could round the frac values as you wish inside the ddply call.

Upvotes: 2

Related Questions