1.数据:nycflights13,如果没有请先安装包(nyctights13 (1) 将flights 和 planes 进行连接,其中,最终结果应当保存 flights 中的所有观测值并且保存 planes 中对应的观测值,并将结果命名为flights_planes (2) 选取flights_planes 中 tailnum 中含有字母”W”的观测值,并将选取出来 的数据框保存为新的 flights_planes (3)剔除flights_planes 中engines 和 seats 缺失的观测值,然后将数据按照 engines 和seats 分组,计算各组飞机在2013 年的distance 和 arrive_delay 的总和,计算结果不能为 NA,将计算结果保存为新的数据 flights_sum (4) 利用上一问所得的flights_sum,计算不同 engines 的总到达迟到时间和 总飞行距离,用r语言完成
时间: 2023-12-06 19:39:34 浏览: 25
首先,我们需要安装并加载 `nycflights13` 包,并加载 `dplyr` 包用于数据处理。
```R
install.packages("nycflights13")
library(nycflights13)
library(dplyr)
```
### 连接 flights 和 planes
我们可以使用 `left_join()` 函数将 `flights` 和 `planes` 数据框连接起来。连接条件是 `tailnum`。
```R
flights_planes <- left_join(flights, planes, by = "tailnum")
```
### 选取 tailnum 中含有字母”W”的观测值
我们可以使用 `filter()` 函数来选取 `tailnum` 中含有字母”W”的观测值,并将结果保存为新的 `flights_planes` 数据框。
```R
flights_planes <- filter(flights_planes, grepl("W", tailnum))
```
### 剔除缺失值并按 engines 和 seats 分组计算
我们可以使用 `filter()` 函数剔除 `engines` 和 `seats` 缺失的观测值,并使用 `group_by()` 函数按照 `engines` 和 `seats` 分组。然后使用 `summarise()` 函数计算每组飞机在2013年的 `distance` 和 `arrive_delay` 的总和。
```R
flights_sum <- flights_planes %>%
filter(!is.na(engines), !is.na(seats)) %>%
group_by(engines, seats) %>%
summarise(total_distance = sum(distance, na.rm = TRUE),
total_arrive_delay = sum(arr_delay, na.rm = TRUE))
```
### 计算不同 engines 的总到达迟到时间和总飞行距离
我们可以使用 `summarise()` 函数计算不同 `engines` 的总到达迟到时间和总飞行距离。
```R
flights_sum_engines <- flights_sum %>%
group_by(engines) %>%
summarise(total_distance = sum(total_distance, na.rm = TRUE),
total_arrive_delay = sum(total_arrive_delay, na.rm = TRUE))
```
最终结果保存在 `flights_sum_engines` 中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)