如何计算';TIBLE'的特定变量的值的数目;在R的间隔内?
需要的包裹 “dplyr” “nycflights13” 我使用的tibble是如何计算';TIBLE'的特定变量的值的数目;在R的间隔内?,r,dplyr,R,Dplyr,需要的包裹 “dplyr” “nycflights13” 我使用的tibble是 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<-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)
给我
# 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
#一个tible:342x2
道具
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
# ... 还有332行
是否有一种方法可以(不使用强力逻辑,如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
#一个tibble:xy
道具
1 0-0.1 45#随机数
2 0.1-0.2 54
3 0.2-0.3 23
在下面,我使用cut
对数据进行装箱,然后使用table
对每个箱子的实例进行计数
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
下面,我使用
cut
对数据进行分类,然后使用table
对每个分类的实例进行计数
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
您可以在
q4%变异后使用(prop=cancelled/totalflights)
:
我相信它会起作用您可以在
q4%变异(prop=cancelled/totalflights)
后使用:
我相信它会成功的我自己想出了一个,我也觉得这是最好的
q4%>%ungroup()%>%count(cut_width(prop,0.025))
输出:
# A tibble: 11 x 2
`cut_width(prop, 0.025)` n
<fct> <int>
1 [-0.0125,0.0125] 233
2 (0.0125,0.0375] 66
3 (0.0375,0.0625] 26
4 (0.0625,0.0875] 13
5 (0.0875,0.112] 14
6 (0.112,0.138] 4
#一个tible:11 x 2
`切割宽度(支柱,0.025)`n
1 [-0.0125,0.0125] 233
2 (0.0125,0.0375] 66
3 (0.0375,0.0625] 26
4 (0.0625,0.0875] 13
5 (0.0875,0.112] 14
6 (0.112,0.138] 4
我自己想出了一个,我也觉得这是最好的
q4%>%ungroup()%>%count(cut_width(prop,0.025))
输出:
# A tibble: 11 x 2
`cut_width(prop, 0.025)` n
<fct> <int>
1 [-0.0125,0.0125] 233
2 (0.0125,0.0375] 66
3 (0.0375,0.0625] 26
4 (0.0625,0.0875] 13
5 (0.0875,0.112] 14
6 (0.112,0.138] 4
#一个tible:11 x 2
`切割宽度(支柱,0.025)`n
1 [-0.0125,0.0125] 233
2 (0.0125,0.0375] 66
3 (0.0375,0.0625] 26
4 (0.0625,0.0875] 13
5 (0.0875,0.112] 14
6 (0.112,0.138] 4
获取以下错误:mutate_impl(.data,dots)中的错误:Columnlabels
必须是长度365(行数)或1,而不是3如果没有“labels”,则可以执行此操作。获取以下错误:mutate_impl(.data,dots)中的错误:Columnlabels
必须是长度365(行数)或者一个,而不是3你可以不用“标签”来做这件事。