Replace NA with Zero in dplyr without using list()

后端 未结 4 1349
Happy的楠姐
Happy的楠姐 2020-12-09 18:07

In dplyr I can replace NA with 0 using the following code. The issue is this inserts a list into my data frame which screws up further analysis down the line. I don\'t even

相关标签:
4条回答
  • 2020-12-09 18:23

    To replace all NAs in a dataframe use

    df %>% replace(is.na(.), 0)

    0 讨论(0)
  • 2020-12-09 18:25
    dt  <- mutate(dt, x = ifelse(is.na(x), 0, x))
    
    0 讨论(0)
  • 2020-12-09 18:38

    What version of dplyr are you using? It might be an old one. The replace_na function now seems to be in tidyr. This works

    library(tidyr)
    df <- tibble::tibble(x = c(1, 2, NA), y = c("a", NA, "b"), z = list(1:5, NULL, 10:20))
    df %>% replace_na(list(x = 0, y = "unknown")) %>% str()
    # Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3 obs. of  3 variables:
    #  $ x: num  1 2 0
    #  $ y: chr  "a" "unknown" "b"
    #  $ z:List of 3
    #   ..$ : int  1 2 3 4 5
    #   ..$ : NULL
    #   ..$ : int  10 11 12 13 14 15 16 17 18 19 ...
    

    We can see the NA values have been replaced and the columns x and y are still atomic vectors. Tested with tidyr_0.7.2.

    0 讨论(0)
  • 2020-12-09 18:43

    For the case of .xlsx, I placed an answer here.

    #install.packages("xlsx")
    library(xlsx)
    extracted_df <- read.xlsx("test.xlsx", sheetName='Sheet1', stringsAsFactors=FALSE)
    # Replace all NAs in a data frame with "G" character
    extracted_df[is.na(extracted_df)] <- "G"
    
    0 讨论(0)
提交回复
热议问题