帮我修改以下代码的错误: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(arrival))
时间: 2024-03-07 22:50:16 浏览: 76
这段代码的问题在于在`summarize()`函数中引用了一个`arrival`对象,但是在代码中并没有定义或加载它。您需要在`summarize()`函数中增加一个`arrive`参数,以便正确引用`arrival`对象。请使用以下代码替换最后一行:
```
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))
```
这里我们将`n_distinct(arrival)`改为`n_distinct(dest)`,因为`arrival`对象未定义。现在,代码应该可以成功运行并生成一个包含`delay_mean`、`delay_var`和`dest_count`列的数据框,其中每个行代表一个特定日期和时间段的统计数据。
阅读全文