r - Dividing column values in group and finding standard deviation of that group
问题描述
I have a data frame as given.
The image contains two columns NAME and RANGE. range have values starting from 50000 to 70000 I want to dived Range in the group of 2000 like from 5000 to 52000 whichever ever value comes that should fall in that group and then I want to find the standard deviation of that group.
I was using the following code
tally(group_by(df,RANGE=cut(RANGE,breaks = seq(50000,70000,by=2000,))) %>%
ungroup() %>%
spread(RANGE,n,fill = 0)
but I am not able to calculate S.d from this
I want my output as follow
RANGE FREQ S.D
50K-52K 10 1.2
52K-54K 5 0.8
....
...
68K-70K 4 2
解决方案
您可以尝试cut
RANGE
分组,然后参加sd
每个小组。
library(dplyr)
df %>%
group_by(group = cut(RANGE, breaks = seq(50000,70000,by=2000))) %>%
summarise(sd = sd(RANGE),
Freq = n())
或类似使用base Raggregate
df$groups <- cut(df$RANGE,breaks = seq(50000,70000,by=2000))
aggregate(RANGE~groups, df, function(x) c(sd(x), length(x)))
推荐阅读
- c# - 相同类型的多个导航属性的关系配置?
- java - File.listFiles() 不断为 File[] 返回 null
- node.js - 有没有办法将 Apollo GraphQL 订阅与 ShareDB 一起使用,而不是使用常规的 WS 连接?
- powershell - PowerShell 和 VSCODE 的问题
- asp.net - URL 的 cookie 属性的域值是什么
- javascript - Sequelize 使用 [Op.or] 查询父模型和包含模型
- angular - Angular ViewChild
- java - 从 (PhoneAuthProvider.getInstance().verifyPhoneNumber() 获取错误,他们在 get 实例和 verifyphonenumber 上制作一条带,如下所示
- google-cloud-firestore - 当我选择任何项目时,我想制作一个微调器,只需从 firestore 数据库中获取数据并放入 textviews
- reactjs - flow 给出了在 react-native 功能组件中未提及返回数据类型的错误