r - 如何在 R 的区间中计算“小标题的特定变量的值的数量”?
问题描述
需要的包
'dplyr'
'nycflights13'
我正在使用的小标题是
q4<-flights%>%group_by(year,month,day)%>%summarise(cancelled=sum(is.na(dep_time)),avg_delay=mean(arr_delay,na.rm = T),totalflights=n())
q4<-q4%>%mutate(prop=cancelled/totalflights)
使用
q4%>%ungroup()%>%count(prop)
给我
# A tibble: 342 x 2
prop n
<dbl> <int>
1 0 7
2 0.00101 1
3 0.00102 2
4 0.00102 1
5 0.00102 1
6 0.00102 1
7 0.00103 1
8 0.00103 1
9 0.00104 1
10 0.00104 1
# ... with 332 more rows
有没有一种方法可以(不使用 for 循环等蛮力逻辑)以所需的形式获得输出,我正在寻找单行或两行解决方案。dplyr 中是否有一个功能可以做到这一点?
期望的输出:
# A tibble: X x Y
prop n
<dbl> <int>
1 0-0.1 45 #random numbers
2 0.1-0.2 54
3 0.2-0.3 23
解决方案
下面,我使用cut
bin 数据,然后table
计算每个 bin 的实例。
data.frame(cut(q4$prop, breaks = c(0, 0.1, 0.2, 0.3)) %>% table)
生产
# . Freq
# 1 (0,0.1] 341
# 2 (0.1,0.2] 13
# 3 (0.2,0.3] 2
推荐阅读
- android - 安卓 | 为什么在具有相同分辨率和相同屏幕尺寸的两部不同手机上,活动看起来会有所不同?
- angular - Angular迭代对象数组并显示其数据
- sql - 在 VBA 中从 SQL 中搜索特定数据
- javascript - 动态表单的 jquery 验证中的错误放置
- scenekit - ARKit 在低光场景中对捕获的图像进行色彩校正
- git - java项目,应该将MANIFEST.MF文件添加到源代码管理中还是忽略?
- arrays - Google Sheets ArrayFormula 性能问题
- vba - 将 Outlook 文件夹中的每周电子邮件附件保存到硬盘
- python - Statsmodels Logit 模型表现良好,但 sklearn LogisticRegression 模型表现在基线分数
- itext7 - 无法写入核心转储。核心转储已被禁用。要启用核心转储,请在使用 Itext7 再次启动 Java 之前尝试“ulimit -c unlimited”