-1

I am trying to create a function to fill NAs in a data frame with values I created using the Mice package.

I used this previous answer to create code that works fine on its own: Use apply on a dataframe to fill in missing values from another dataframe

Here is my working code:

inds <- match(df_raw$id,miceoutput$id)
df_raw$PIDS_14 <- ifelse(is.na(df_raw$PIDS_14), miceoutput$PIDS_14[inds], df_raw$PIDS_14)

However, when I try to create a function that does the same thing, using this code:

impute <- function(x) {
   df_raw$x <- ifelse(is.na(df_raw$x) == TRUE, miceoutput$x[inds],df_raw$x)
}

I get the following error when I run

impute(PIDS_14)

 Error in `$<-.data.frame`(`*tmp*`, x, value = logical(0)) : replacement has 0 rows, data has 
77 

I cannot figure out why it would work fine as a stand alone line of code, but the same thing will not work in the function. Any help appreciated.

Steven
  • 73
  • 5
  • Try with `df_raw[[x]]`instead of `df_raw$x` and pass the variable name as a quoted string: `impute("PIDS14")`. And of course `miceoutput[[x]][inds]`. – stefan Jul 27 '21 at 20:52
  • I ran the code suggested by @stefan and it ran without error. However, it did not actually change the value in the data frame. – Steven Jul 27 '21 at 22:23

1 Answers1

0

Functions behave a little differently. It is not a good practice to change dataframes within the function, return the changed dataframe from the function and pass the column name as string.

impute <- function(x) {
  df_raw[[x]] <- ifelse(is.na(df_raw[[x]]), miceoutput[[x]][inds],df_raw[[x]])
  df_raw
}

df_raw <- impute("PIDS_14")
df_raw
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you so much @Ronak that seems to have worked. I am still new to functions and this will help as I learn! – Steven Jul 28 '21 at 14:31