用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值
时间: 2023-09-17 19:14:22 浏览: 64
以下是代码实现:
```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`函数选择需要输出的列。
相关问题
在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语言,按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)
可以使用dplyr包来完成该任务。代码如下:
```R
library(dplyr)
# 读入数据
df <- read.csv("flights.csv")
# 按dest分组,去掉NA值,计算arr_delay>0的比例
prop1 <- df %>%
filter(!is.na(dest)) %>%
group_by(dest) %>%
summarise(prop1 = mean(arr_delay > 0))
# 按dest分组,去掉NA值和arr_delay为负数的值,计算arr_delay>0的比例
prop2 <- df %>%
filter(!is.na(dest) & arr_delay >= 0) %>%
group_by(dest) %>%
summarise(prop2 = mean(arr_delay > 0))
# 按dest分组,去掉NA值和50%的arr_delay为NA的值,计算arr_delay>0的比例
prop3 <- df %>%
group_by(dest) %>%
mutate(na_prop = mean(is.na(arr_delay))) %>%
filter(!is.na(dest) & is.na(arr_delay) | (arr_delay >= 0 & !is.na(arr_delay))) %>%
group_by(dest) %>%
summarise(prop3 = mean(arr_delay > 0, na.rm = TRUE) * (1 - na_prop) + 0.5 * na_prop)
# 合并数据
result <- prop1 %>%
left_join(prop2, by = "dest") %>%
left_join(prop3, by = "dest") %>%
mutate(diff1 = prop2 - prop1, diff2 = prop3 - prop1) %>%
arrange(dest)
# 输出结果
write.csv(result, "result.csv", row.names = FALSE)
```
相关推荐
![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)
![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)