My data is this:
train <- data.frame(y=c(1,2,1,1), x1=c(2,4,NA,5), x2=c(8,NA,6,12))
I need to replace for each x variable the missing values (NAs) with the mean of that column but the mean must be calculated using the values of that x variable that have a corresponding y value equal to the y value of the row of that missing value.
For instance: in the row where the NA of the x1 column is, the y value is equal to 1, so this missing value should be replaced with the mean between 2 and 5 (which are the x1 values for which y is also 1).
My code is like this but the mean is not conditional:
for(i in 1:ncol(train)){
train[is.na(train[,i]), i] <- mean(train[,i], na.rm = TRUE)
}
library(dplyr)
train %>%
group_by(y) %>%
mutate_at(vars(-y), function(v){
if_else(is.na(v), mean(v, na.rm = TRUE), v)
}) %>%
ungroup()
## A tibble: 4 x 3
# y x1 x2
# <dbl> <dbl> <dbl>
#1 1 2 8
#2 2 4 NaN
#3 1 3.5 6
#4 1 5 12
We can use na.aggregate
after grouping by the 'y' column
library(dplyr)
library(zoo)
train %>%
group_by(y) %>%
mutate_at(vars(-one_of(group_vars(.))),
~if(all(is.na(.))) NA_real_ else na.aggregate(.))
# A tibble: 4 x 3
# Groups: y [2]
# y x1 x2
# <dbl> <dbl> <dbl>
#1 1 2 8
#2 2 4 NA
#3 1 3.5 6
#4 1 5 12
Or apply na.aggregate
after split
ting the dataset into a list
of data.frame
s based on 'y' column
train[-1] <- unsplit(lapply(split(train[-1], train$y), na.aggregate), train$y)
Consider ave
for groupwise mean wrapped inside an ifelse
for NA
condition or not:
# ITERATE THROUGH ALL COLUMNS BUT FIRST
for(i in c("x1", "x2")) {
train[[i]] <- ifelse(test = is.na(train[[i]]),
yes = ave(train[[i]], train$y, FUN=function(x) mean(x, na.rm=TRUE)),
no = train[[i]])
}
train
# y x1 x2
# 1 1 2.0 8
# 2 2 4.0 NaN
# 3 1 3.5 6
# 4 1 5.0 12
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.