Search code examples
rdataframenamissing-dataimputation

Replace all NA with FALSE in selected columns in R


I have a question similar to this one, but my dataset is a bit bigger: 50 columns with 1 column as UID and other columns carrying either TRUE or NA, I want to change all the NA to FALSE, but I don't want to use explicit loop.

Can plyr do the trick? Thanks.

UPDATE #1

Thanks for quick reply, but what if my dataset is like below:

df <- data.frame(
  id = c(rep(1:19),NA),
  x1 = sample(c(NA,TRUE), 20, replace = TRUE),
  x2 = sample(c(NA,TRUE), 20, replace = TRUE)
)

I only want X1 and X2 to be processed, how can this be done?


Solution

  • If you want to do the replacement for a subset of variables, you can still use the is.na(*) <- trick, as follows:

    df[c("x1", "x2")][is.na(df[c("x1", "x2")])] <- FALSE
    

    IMO using temporary variables makes the logic easier to follow:

    vars.to.replace <- c("x1", "x2")
    df2 <- df[vars.to.replace]
    df2[is.na(df2)] <- FALSE
    df[vars.to.replace] <- df2