How to remove rows with inf from a dataframe in R

后端 未结 6 639
自闭症患者
自闭症患者 2020-12-29 03:50

I have a very large dataframe(df) with approximately 35-45 columns(variables) and rows greater than 300. Some of the rows contains NA,NaN,Inf,-Inf values in

6条回答
  •  情书的邮戳
    2020-12-29 04:13

    It took me awhile to work this out for dplyr 1.0.0 so i thought i would put up the new version of @sbha solutions using c_across since filter_all, filter_if are getting deprecated.

    library(dplyr)
    df <- tibble(a = c(1, 2, 3, NA), b = c(5, Inf, 8, 8), c = c(9, 10, Inf, 11), d = c('a', 'b', 'c', 'd'))
    #       a     b     c d    
    #      
    # 1     1     5     9 a    
    # 2     2   Inf    10 b    
    # 3     3     8   Inf c    
    # 4    NA     8    11 d 
    
    df %>% 
      rowwise %>% 
      filter(!all(is.infinite(c_across(where(is.numeric)))))
    # # A tibble: 4 x 4
    # # Rowwise: 
    #       a     b     c d    
    #      
    # 1     1     5     9 a    
    # 2     2   Inf    10 b    
    # 3     3     8   Inf c    
    # 4    NA     8    11 d 
    
    df %>% 
      rowwise %>% 
      filter(!any(is.infinite(c_across(where(is.numeric)))))
    # # A tibble: 2 x 4
    # # Rowwise: 
    #       a     b     c d    
    #      
    # 1     1     5     9 a    
    # 2    NA     8    11 d 
    
    df %>% 
      rowwise %>% 
      filter(!any(is.infinite(c_across(a:c))))
    
    # # A tibble: 2 x 4
    # # Rowwise: 
    #       a     b     c d    
    #      
    # 1     1     5     9 a    
    # 2    NA     8    11 d 
    

    To be honest I think @sbha answer is simpler!

提交回复
热议问题