在R中,如何将NA值替换为组的平均值?

I have two data frames, the first (dat) has two columns group and value. The second data frame (dat2) has the mean value for each group. How would I replace the NA values in dat with the mean value of the group found in dat2?

library(dplyr)

x <- as.data.frame(seq(from = 1, to = 10, by = 1))
names(x)[1] <- "value"
y <- as.data.frame(c("A","A","A","A","A","B","B","B","B","B"),1)
names(y)[1] <- "group"

dat <- cbind(y,x)

dat[dat == 3] <- NA
dat[dat == 7] <- NA

dat2 <- dat %>%
  group_by(group) %>%
  summarise_at(.vars = names(.)[2],.funs = c(mean="mean"),na.rm=TRUE)
评论
Toby
Toby

这个怎么样?

library(dplyr)

x <- as.data.frame(seq(from = 1, to = 10, by = 1))
names(x)[1] <- "value"
y <- as.data.frame(c("A","A","A","A","A","B","B","B","B","B"),1)
names(y)[1] <- "group"

dat <- cbind(y,x)

dat[dat == 3] <- NA
dat[dat == 7] <- NA

dat %>%
  group_by(group) %>%
  mutate(
    value = ifelse(is.na(value), mean(value, na.rm = T), value)
  ) %>%
  ungroup()
点赞
评论