用r语言完成,在flights中,按dest分组,考虑arr_delay列,去除dest列中NA值。将dest按字母顺序,由小至大,求每组中arr_delay>0的比例,分三种情况,不去除NA值,去除NA值,NA值的个数只按50%计算。如(1,2,-1,NA,NA)计算结果0.4, 0.666,0.5. 三列的名字为prop1,prop2, prop3. 并计算两者的差值diff1=prop2-prop1 和diff2=prop3-prop1(最后列名字不能错,顺序也不能错,dest,prop1,prop2,prop3,diff1,diff2)
时间: 2023-09-17 13:14:22 浏览: 39
以下是代码实现:
```r
library(dplyr)
flights %>%
filter(!is.na(dest)) %>%
group_by(dest) %>%
summarize(prop1 = mean(arr_delay > 0),
prop2 = mean(arr_delay > 0, na.rm = TRUE),
prop3 = mean(arr_delay > 0, na.rm = sum(!is.na(arr_delay))/2)) %>%
arrange(dest) %>%
mutate(diff1 = prop2 - prop1, diff2 = prop3 - prop1)
```
解释一下代码:
- 首先使用 `filter(!is.na(dest))` 去除 `dest` 列中的 `NA` 值。
- 然后使用 `group_by(dest)` 按 `dest` 分组。
- 使用 `summarize()` 函数计算每组的三种情况下的比例,分别命名为 `prop1`、`prop2`、`prop3`。
- 使用 `arrange(dest)` 按 `dest` 字母顺序排序。
- 最后使用 `mutate()` 函数计算 `diff1` 和 `diff2` 两列,分别为 `prop2-prop1` 和 `prop3-prop1`。
相关问题
在flights中,#按dest分组,考虑arr_delay列,去除两列中NA值。将dest按字母顺序,由小至大。新增列rank1,为数据表该行在对应的dest小组中的rank(针对arr_delay列,不用summarise做),新增列rank2,为数据表该行在对应的dest小组中的rank逆排序rank(desc), 新增列diff,为逻辑变量如果rank1>rank2结果为true,否则false。最终结果4列。dest,rank1,rank2,diff。注意题目说rank就用rank命令,如说min_rank就用min_rank命令
以下是可以实现这个功能的代码:
```{r}
library(dplyr)
flights %>%
filter(!is.na(arr_delay)) %>% # 去除NA值
group_by(dest) %>%
arrange(dest, arr_delay) %>% # 按字母顺序,arr_delay升序排序
mutate(rank1 = rank(arr_delay), # 计算rank1
rank2 = rank(desc(arr_delay)), # 计算rank2
diff = rank1 > rank2) %>% # 计算diff
select(dest, rank1, rank2, diff) # 选择需要的列
```
解释一下代码:
首先用`filter()`函数去除了`arr_delay`列中的NA值。然后用`group_by(dest)`函数按`dest`列进行分组。接着用`arrange(dest, arr_delay)`函数按字母顺序和`arr_delay`升序排序。然后用`mutate()`函数新增了三列,分别是`rank1`、`rank2`和`diff`。其中`rank1`是`arr_delay`列在对应的`dest`小组中的排名,使用`rank()`函数计算。`rank2`是`arr_delay`列在对应的`dest`小组中的逆序排名,使用`rank(desc(arr_delay))`函数计算。`diff`是逻辑变量,如果`rank1`大于`rank2`则为TRUE,否则为FALSE。最后用`select()`函数选择需要的列,包括`dest`、`rank1`、`rank2`和`diff`。
用r语言完成,在flights中,按dest分组,考虑arr_delay列,去除dest列中NA值。将dest按字母顺序,由小至大,求每组中arr_delay>0的比例,分三种情况,不去除NA值,去除NA值,NA值的个数只按50%计算。如(1,2,-1,NA,NA)计算结果0.4, 0.666,0.5. 三列的名字为prop1,prop2, prop3. 并计算两者的差值diff1=prop2-prop1 和diff2=prop3-prop1(最后列名字不能错,顺序也不能错,dest,prop1,prop2,prop3,diff1,diff2)要求tibble中不含NA值
以下是代码实现:
```r
library(dplyr)
flights %>%
filter(!is.na(dest)) %>%
group_by(dest) %>%
summarize(prop1 = mean(arr_delay > 0),
prop2 = mean(arr_delay > 0, na.rm = TRUE),
prop3 = mean(arr_delay > 0, na.rm = sum(!is.na(arr_delay))/2)) %>%
arrange(dest) %>%
mutate(diff1 = prop2 - prop1,
diff2 = prop3 - prop1) %>%
select(dest, prop1, prop2, prop3, diff1, diff2)
```
首先使用`filter`函数去除`dest`列中的NA值,然后使用`group_by`函数按照`dest`分组,再使用`summarize`函数计算每组中`arr_delay>0`的比例,分别对应于`prop1`、`prop2`和`prop3`。其中,`prop1`不去除NA值,`prop2`去除NA值,`prop3`将NA值按照50%计算。最后使用`arrange`函数将`dest`按照字母顺序排序,再使用`mutate`函数计算出`diff1`和`diff2`,最后使用`select`函数选择需要输出的列。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)