帮我修改以下代码的错误:library(nycflights13) library(dplyr) flights <- na.omit(flights, cols = c("dep_time")) flights$dep_interval <- ifelse(flights$dep_time >= 601 & flights$dep_time <= 1200, "上午", ifelse(flights$dep_time > 1200 & flights$dep_time <= 1800, "下午", ifelse(flights$dep_time > 1800 & flights$dep_time <= 2400, "晚上", ifelse(flights$dep_time > 1 & flights$dep_time <= 600, "凌晨", NA)))) flights_new <- flights %>% na.omit() %>% group_by(year, month, day, dep_interval) %>% summarize("delay_mean" = mean(arr_delay, na.rm = TRUE), "delay_var" = var(arr_delay, na.rm = TRUE), "dest_count" = n_distinct(dest)) flights_select <- flights_new %>% filter(mean_arr_delay <= 30, num_airports >= 50) %>% arrange(desc(mean_arr_delay))
时间: 2024-03-07 15:50:17 浏览: 73
flights-tracker:www.flights-tracker.com
这段代码的问题在于`filter()`函数中引用了一个名为`mean_arr_delay`的未定义对象,正确的对象名应该是`delay_mean`。此外,在`filter()`函数中引用了一个名为`num_airports`的未定义对象。我们需要在`summarize()`函数中增加一个名为`num_airports`的参数,以便正确引用`dest_count`列。
请使用以下代码替换最后一行:
```
flights_select <- flights_new %>%
filter(delay_mean <= 30, dest_count >= 50) %>%
arrange(desc(delay_mean))
```
这里我们将`mean_arr_delay`改为`delay_mean`,并将`num_airports`改为`dest_count`,以与之前的`summarize()`函数中的列名匹配。
现在,代码应该可以成功运行并产生一个数据框,其中按平均延误降序排列,并且平均延误小于或等于30分钟,出发机场数量大于或等于50个。
阅读全文