How do I replace NA values with zeros in an R dataframe?

后端 未结 20 1870
说谎
说谎 2020-11-21 23:41

I have a data frame and some columns have NA values.

How do I replace these NA values with zeroes?

相关标签:
20条回答
  • 2020-11-22 00:15

    Another dplyr pipe compatible option with tidyrmethod replace_na that works for several columns:

    require(dplyr)
    require(tidyr)
    
    m <- matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10)
    d <- as.data.frame(m)
    
    myList <- setNames(lapply(vector("list", ncol(d)), function(x) x <- 0), names(d))
    
    df <- d %>% replace_na(myList)
    

    You can easily restrict to e.g. numeric columns:

    d$str <- c("string", NA)
    
    myList <- myList[sapply(d, is.numeric)]
    
    df <- d %>% replace_na(myList)
    
    0 讨论(0)
  • 2020-11-22 00:17

    You can use replace()

    For example:

    > x <- c(-1,0,1,0,NA,0,1,1)
    > x1 <- replace(x,5,1)
    > x1
    [1] -1  0  1  0  1  0  1  1
    
    > x1 <- replace(x,5,mean(x,na.rm=T))
    > x1
    [1] -1.00  0.00  1.00  0.00  0.29  0.00 1.00  1.00
    
    0 讨论(0)
  • 2020-11-22 00:18

    dplyr example:

    library(dplyr)
    
    df1 <- df1 %>%
        mutate(myCol1 = if_else(is.na(myCol1), 0, myCol1))
    

    Note: This works per selected column, if we need to do this for all column, see @reidjax's answer using mutate_each.

    0 讨论(0)
  • 2020-11-22 00:19

    For a single vector:

    x <- c(1,2,NA,4,5)
    x[is.na(x)] <- 0
    

    For a data.frame, make a function out of the above, then apply it to the columns.

    Please provide a reproducible example next time as detailed here:

    How to make a great R reproducible example?

    0 讨论(0)
  • 2020-11-22 00:19

    It is also possible to use tidyr::replace_na.

        library(tidyr)
        df <- df %>% mutate_all(funs(replace_na(.,0)))
    
    0 讨论(0)
  • 2020-11-22 00:19

    if you want to assign a new name after changing the NAs in a specific column in this case column V3, use you can do also like this

    my.data.frame$the.new.column.name <- ifelse(is.na(my.data.frame$V3),0,1)
    
    0 讨论(0)
提交回复
热议问题