首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:元素的分数是多少?

R:元素的分数是多少?
EN

Stack Overflow用户
提问于 2015-12-09 15:09:54
回答 1查看 98关注 0票数 0

我有一个3列的数据框架。需要对其进行总结,并找到每个元素记录w.r.t的一小部分。它的分组中记录的总数(不是全部)。摘要在ddply中很好,但现在我只能使用分数。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
                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

期望产出:

代码语言:javascript
复制
                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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-09 15:27:16

您可以这样做,使用plyr

代码语言:javascript
复制
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值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34182210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档