How to replace empty string with NA in R dataframe?

前端 未结 1 536
礼貌的吻别
礼貌的吻别 2020-12-15 07:44

My first approach was to use na.strings=\"\" when I read the data in from a csv. This doesn\'t work for some reason. I also tried:

df[df==\'\']          


        
相关标签:
1条回答
  • 2020-12-15 08:35

    I'm not sure why df[df==""]<-NA would have not worked for OP. Let's take a sample data.frame and investigate options.

    Option#1: Base-R

    df[df==""]<-NA
    
    df
    #    One  Two Three Four
    # 1    A    A  <NA>  AAA
    # 2 <NA>    B    BA <NA>
    # 3    C <NA>    CC  CCC
    

    Option#2: dplyr::mutate_all and na_if. Or mutate_if if data frame got multiple types of columns

    library(dplyr)
    
    mutate_all(df, list(~na_if(.,"")))
    

    OR

    #if data frame other types of character Then
    df %>% mutate_if(is.character, list(~na_if(.,""))) 
    
    #    One  Two Three Four
    # 1    A    A  <NA>  AAA
    # 2 <NA>    B    BA <NA>
    # 3    C <NA>    CC  CCC
    

    Toy Data:

    df <- data.frame(One=c("A","","C"), 
                     Two=c("A","B",""), 
                     Three=c("","BA","CC"), 
                     Four=c("AAA","","CCC"), 
                     stringsAsFactors = FALSE)
    
    df
    #   One Two Three Four
    # 1   A   A        AAA
    # 2       B    BA     
    # 3   C        CC  CCC
    
    0 讨论(0)
提交回复
热议问题