1

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?

  • Have you seen [this answer](http://stackoverflow.com/a/9322975/1305688)? 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](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Dec 29 '15 at 19:43

2 Answers2

1

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?

Steven_
  • 738
  • 2
  • 7
  • 20
  • 1
    Thank you, this is exactly what I needed! Somehow defining namestoChange did not work for me, probably because my imputed data frame had another dimension, but I have just defined it as `namestoChange <- colnames(imputed)` – mrsdalloway Dec 29 '15 at 22:02
  • glad I could help :) – Steven_ Dec 30 '15 at 00:12
  • I would be a little careful with this and keep in mind that your data would need to be in the exact same sort order for this to be useful. I used it to replace data with the same variable name, but because the new data was in a different order than my old data, I ended up scrambling up my data. Therefore for my needs, I used merge and removed the .x variables (and then removed the .y from the variable names). – ktmbiome Aug 10 '20 at 19:20
0
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   
Perceptron
  • 399
  • 3
  • 11