【问题标题】:Using filter in dplyr to generate values for all rows在 dplyr 中使用过滤器为所有行生成值
【发布时间】:2017-12-12 22:42:28
【问题描述】:
library(tidyverse)
library(nycflights13)
nycflights13::flights

如果以下表达式给出数据集中每天的航班:

daily <- dplyr::group_by( flights, year, month, day)
(per_day <- dplyr::summarize( daily, flights = n()))

我想为取消的航班提供类似的东西:

canx <- dplyr::filter( flights, is.na(dep_time) & is.na(arr_time))
canx2 <- canx %>% dplyr::group_by( year, month, day) 

我的目标是拥有与所有汇总航班相同长度的数据框。

我可以获得每天取消的航班数量:

(canx_day <- dplyr::summarize( canx2, flights = n()))

但显然这是一个稍短的数据帧,所以我不能运行例如:

canx_day$propcanx <- per_day$flights/canx_day$flights

即使我引入了 NA,我也可以替换它们。

所以我的问题是,我应该不使用过滤器,还是应该应用过滤器的参数?

非常感谢

【问题讨论】:

  • 是否有Cancelled 的列来标记取消的航班?如果是,那么你的工作就轻松多了。

标签: r dplyr


【解决方案1】:

您不应该使用filter。正如其他人所建议的那样,使用canceled 列很容易,因此我们的第一步是创建该列。然后,您可以通过单个summarize 轻松获得您想要的任何内容。例如:

flights %>% 
    mutate(canceled = as.integer(is.na(dep_time) & is.na(arr_time))) %>%
    group_by(year, month, day) %>%
    summarize(n_scheduled = n(),
              n_not_canceled = sum(!canceled),
              n_canceled = sum(canceled),
              prop_canceled = mean(canceled))
# # A tibble: 365 x 7
# # Groups:   year, month [?]
#     year month   day n_scheduled n_not_canceled n_canceled prop_canceled
#    <int> <int> <int>       <int>          <int>      <int>         <dbl>
#  1  2013     1     1         842            838          4   0.004750594
#  2  2013     1     2         943            935          8   0.008483563
#  3  2013     1     3         914            904         10   0.010940919
#  4  2013     1     4         915            909          6   0.006557377
#  5  2013     1     5         720            717          3   0.004166667
#  6  2013     1     6         832            831          1   0.001201923
#  7  2013     1     7         933            930          3   0.003215434
#  8  2013     1     8         899            895          4   0.004449388
# ...

【讨论】:

  • 不错。谢谢!
【解决方案2】:

这会为您提供flight, year, month, day 每天的航班和取消航班

nycflights13::flights %>% 
  group_by(flight, year, month, day) %>% 
  summarize(per_day = n(),
            canx = sum(ifelse(is.na(arr_time), 1, 0)))

【讨论】:

    【解决方案3】:

    有一种简单的方法可以计算每天取消的航班数量。假设取消航班的Cancelled 列是TRUE。如果是这样,那么计算每日取消航班的方法是:

    flights %>%
    group_by(year, month, day) %>%
    summarize( canx_day = sum(Cancelled))
    

    canx_day 将包含一天的取消航班。

    【讨论】:

    • 请为 cmets 提供 -ve 票。上面的解决方案回答了 OP 关于使用filter 的问题,并展示了一种在假设Cancelled 列的情况下计算取消航班的方法。即使是公认的解决方案也采用了相同的方法,但包括添加 canceled 列的方式。
    • 没有投反对票,但提供了数据(在问题中加载的包中),因此很容易看出没有“取消”列。您没有直接解决 OP 提出的问题(“所以我的问题是,我应该不使用过滤器,还是有参数可以过滤我应该应用的过滤器?”),并且 OP 在问题是他们能拿到取消航班的数量,主要的障碍似乎是拿到取消航班的比例。对于你我来说,这是一个简单的扩展,但对于 OP 来说,这是问题的一部分。
    • 在我看来,这些是不赞成而不是反对的原因,但不同的人有不同的标准。这些因素的某种组合是我对你为什么要投反对票的最佳猜测。
    • @Gregor 感谢您分享您的观点,我同意您的观点。正如我之前提到的,我的观点是计数有困难,因此提供了一个带有假设的解决方案。只要我能够提供帮助,赞成/反对票并不重要。如果您的解决方案已经存在,我可能不会发布解决方案,因为您已经非常清楚地解释了。
    • 是的,我在写完自己的解决方案时看到了您的解决方案。由于基本的方法是一样的,本来想不贴的,但我觉得我的解释对OP来说会更清楚。
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多