【问题标题】:Need to count the quantity of a specific number in a column, with a filter需要统计一列中特定数字的数量,带过滤器
【发布时间】:2020-08-02 04:31:16
【问题描述】:

我使用了来自“nycflights13”一部分的 2 个数据集的数据,并用它来找出从特定机场起飞的最旧航班。现在我需要找出有多少这种类型的飞机飞出机场。我不知道该怎么做。我尝试使用 table() 但这给了我 1 的输出。

library(nycflights13)

library(conflicted)  
suppressMessages(conflict_prefer("filter", "dplyr"))  
suppressPackageStartupMessages(library(tidyverse)) 

data("flights") 

theflights <- 
   flights %>% 
   filter(origin == "JFK") %>%
   select (tailnum)  

 data("planes") 

 x <- inner_join(theflights, planes) %>%  
  filter(year == min(year, na.rm = T)) %>%  
  select(tailnum) %>%  
  distinct()
x

这是问题第一部分的原始代码。我认为 table() 不起作用的原因是因为我将 x 创建为单个值。我如何知道该类型的飞机从特定机场起飞的次数?

【问题讨论】:

  • 你能显示你用于table 的代码吗? x 只有一行。你想用它做什么?

标签: r tidyr tidy


【解决方案1】:

我不确定我是否理解正确,但我认为这听起来像您在问如何使用您存储在x 中的tailnum 获取航班数量,飞出"JFK"?如果是这种情况,您可以根据这两个条件进行过滤,然后获取行数:

library(nycflights13)
library(dplyr)

filter(flights, tailnum == "N381AA" & origin == "JFK") %>% 
  nrow()
#> [1] 22

reprex package (v0.3.0) 于 2020 年 8 月 2 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    相关资源
    最近更新 更多