Delete rows with blank values in one particular column

后端 未结 5 1935
Happy的楠姐
Happy的楠姐 2020-12-12 12:56

I am working on a large dataset, with some rows with NAs and others with blanks:

df <- data.frame(ID = c(1:7),                                   
                 


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

    An easy approach would be making all the blank cells NA and only keeping complete cases. You might also look for na.omit examples. It is a widely discussed topic.

    df[df==""]<-NA
    df<-df[complete.cases(df),]
    
    0 讨论(0)
  • 2020-12-12 13:39
     df[!(is.na(df$start_pc) | df$start_pc==""), ]
    
    0 讨论(0)
  • 2020-12-12 13:48

    It is the same construct - simply test for empty strings rather than NA:

    Try this:

    df <- df[-which(df$start_pc == ""), ]
    

    In fact, looking at your code, you don't need the which, but use the negation instead, so you can simplify it to:

    df <- df[!(df$start_pc == ""), ]
    df <- df[!is.na(df$start_pc), ]
    

    And, of course, you can combine these two statements as follows:

    df <- df[!(df$start_pc == "" | is.na(df$start_pc)), ]
    

    And simplify it even further with with:

    df <- with(df, df[!(start_pc == "" | is.na(start_pc)), ])
    

    You can also test for non-zero string length using nzchar.

    df <- with(df, df[!(nzchar(start_pc) | is.na(start_pc)), ])
    

    Disclaimer: I didn't test any of this code. Please let me know if there are syntax errors anywhere

    0 讨论(0)
  • 2020-12-12 13:49

    An elegant solution with dplyr would be:

    df %>%
      # recode empty strings "" by NAs
      na_if("") %>%
      # remove NAs
      na.omit
    
    0 讨论(0)
  • 2020-12-12 13:49

    Alternative solution can be to remove the rows with blanks in one variable:

    df <- subset(df, VAR != "")
    
    0 讨论(0)
提交回复
热议问题