Replace all NA with FALSE in selected columns in R

前端 未结 5 1782
挽巷
挽巷 2020-12-31 01:57

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

相关标签:
5条回答
  • 2020-12-31 02:28

    You can use the NAToUnknown function in the gdata package

    df[,c('x1', 'x2')] = gdata::NAToUnknown(df[,c('x1', 'x2')], unknown = 'FALSE')
    
    0 讨论(0)
  • 2020-12-31 02:37

    tidyr::replace_na excellent function.

    df %>%
      replace_na(list(x1 = FALSE, x2 = FALSE))
    

    This is such a great quick fix. the only trick is you make a list of the columns you want to change.

    0 讨论(0)
  • 2020-12-31 02:38

    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
    
    0 讨论(0)
  • 2020-12-31 02:41

    Try this code:

    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)
    )
    replace(df, is.na(df), FALSE)
    

    UPDATED for an another solution.

    df2 <- 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)
    )
    df2[names(df) == "id"] <- FALSE
    df2[names(df) != "id"] <- TRUE
    replace(df, is.na(df) & df2, FALSE)
    
    0 讨论(0)
  • 2020-12-31 02:50

    With dplyr you could also do

    df %>% mutate_each(funs(replace(., is.na(.), F)), x1, x2)
    

    It is a bit less readable compared to just using replace() but more generic as it allows to select the columns to be transformed. This solution especially applies if you want to keep NAs in some columns but want to get rid of NAs in others.

    0 讨论(0)
提交回复
热议问题