group = c(1,1,4,4,4,5,5,6,1,4,6)
animal = c('a','b','c','c','d','a','b','c','b','d','c')
sleep = c(14,NA,22,15,NA,96,100,NA,50,2,1)
test = data.frame(group, animal, sleep)
print(test)
group_animal = test %>% group_by(`group`, `animal`) %>% summarise(mean_sleep = mean(sleep, na.rm = T))我想根据按组和动物分组的平均睡眠值替换NA值,即睡眠列。
我是否可以执行类似于Excel的某种查找,将测试数据中的组和动物与group_animal数据some相匹配,并将测试df中的睡眠列中的NA值替换为group_animal df中的睡眠值?
发布于 2022-07-26 15:35:52
我们可以使用mutate而不是summarise,因为summarise每组返回一行
library(dplyr)
library(tidyr)
test <- test %>%
group_by(group, animal) %>%
mutate(sleep = replace_na(sleep, mean(sleep, na.rm = TRUE))) %>%
ungroup-output
test
# A tibble: 11 × 3
group animal sleep
<dbl> <chr> <dbl>
1 1 a 14
2 1 b 50
3 4 c 22
4 4 c 15
5 4 d 2
6 5 a 96
7 5 b 100
8 6 c 1
9 1 b 50
10 4 d 2
11 6 c 1https://stackoverflow.com/questions/73126179
复制相似问题