gpt4 book ai didi

r - 用分组变量中存在的值替换 NA

转载 作者:行者123 更新时间:2023-12-04 11:34:01 26 4
gpt4 key购买 nike

虚假数据

fruit <- c("Orange", "Banana", "Orange", "Banana")
flavour <- c("Bitter", NA, NA, "Sweet")
geo <- c(NA, NA, NA, "France")
value <- c(1, NA, NA, 4)
dd <- data.frame(fruit, flavour, geo, value)
rm(fruit, flavour, geo, value)

enter image description here

我想按“水果”对数据集进行分组并替换缺失值在具有分组数据中存在的值的所有变量中。

期望的输出

fruit <- c("Orange", "Banana", "Orange", "Banana")
flavour <- c("Bitter", "Sweet", "Bitter", "Sweet")
geo <- c(NA, "France", NA, "France")
value <- c(1, 4, 1, 4)
dd2 <- data.frame(fruit, flavour, geo, value)
rm(fruit, flavour, geo, value)

enter image description here

代码尝试

tt <- dd %>%
group_by(fruit) %>%
summarise_all()

最佳答案

group_by 之后我们需要mutate_all(summarise/summarise_all 用于从多行中获取单行)。使用 zoo 中的 na.locf 为每一列用相邻的非 NA 元素填充 NA 元素

library(zoo)
library(dplyr)
dd %>%
group_by(fruit) %>%
mutate_all(funs(na.locf(na.locf(., na.rm = FALSE),
fromLast = TRUE, na.rm = FALSE)))
# A tibble: 4 x 4
# Groups: fruit [2]
# fruit flavour geo value
# <fct> <fct> <fct> <dbl>
#1 Orange Bitter <NA> 1
#2 Banana Sweet France 4
#3 Orange Bitter <NA> 1
#4 Banana Sweet France 4

关于r - 用分组变量中存在的值替换 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52059900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com