1
votes

I have used mice package in R to impute some missing values in my data, but not for all variables. Now I would like to replace the columns from the original data with columns from the imputed data, if their column names are equal. Here is my function:

 replace_imp <- function(data,impdata) {
  for(i in 1:length(impdata)){
    for(k in 1:length(data)){
      if(colnames(impdata)[i]==colnames(data)[k]){
        data[,k] <- imp_data[,i]
      }
    }
  }
}

But it does not seem to work, any help?

2
Have you seen this answer? If that doesn't help you I suggest you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. You can have a look at this SO post on how to make a great reproducible example in R. - Eric Fail

2 Answers

1
votes

Starting with a minimal data set:

original <- data.frame(X=c(1, 1, 1), Y=c(2, 2, 2), Z=c(3, 3, 3))
imputed <- data.frame(A=c(2, 2, 2), Y=c(5, 5, 5), Z=c(1, 1, 1))

We should expect the original data frame to change it's 'Y' and 'Z' column to the imputed's value. Let's create a function that takes all matching column names, and for every match, we will replace the original's values with the imputed's.

replace_imputed <- function(original, imputed){

  namestoChange <- colnames(original)[colnames(imputed) %in% colnames(original)]

  for(i in 1:length(namestoChange)){
    original[namestoChange[i]] <- imputed[namestoChange[i]]
  }
  return(original)

}

> replace_imputed(original, imputed)
  X Y Z
1 1 5 1
2 1 5 1
3 1 5 1

Is this more or less what you were looking for?

0
votes
original <- data.frame(X=c(1, 1, 1), Y=c(2, 2, 2), Z=c(3, 3, 3))
imputed <- data.frame(A=c(2, 2, 2), Y=c(5, 5, 5), Z=c(1, 1, 1))

original[names(imputed)] <- imputed

X   Y   Z   A
1   5   1   2   
1   5   1   2   
1   5   1   2