如何在R的间隔中计算“小节的特定变量的值数”?

时间:2018-06-29 17:03:35

标签: r dplyr

所需软件包

'dplyr'

'nycflights13'

我正在使用的小标题

export type UserModel = mongoose.Document & {
  email: string,
  password: string,
  ...
}

使用

mongoose.Document &

给我

 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)

有没有办法(不使用诸如for循环之类的强力逻辑) 以所需的形式获得输出,我正在寻找单行或两行解决方案。 dplyr中有功能吗?

所需的输出:

q4%>%ungroup()%>%count(prop)

3 个答案:

答案 0 :(得分:2)

下面,我使用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

答案 1 :(得分:0)

您可以在q4<-q4%>%mutate(prop=cancelled/totalflights)之后使用:

q4 %>% ungroup() %>%
mutate(category = cut(prop, breaks = c(-Inf,0.1,0.2,Inf), labels = c("0-0.1","0.1-0.2", "0.2 - 0.3") %>%
count(category)

我相信它会起作用

答案 2 :(得分:0)

我自己弄清楚了,我也觉得这是最好的。

           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