Search code examples
rdataframedelete-row

delete row of a dataframe with a specific value


I have a dataframe (df) with 10 columns and more than 10000 rows, I want to delete all rows that contain the value -999, i guess thats simple, So i do:

dfnew=df[!rowSums(df ==-999),]

all my columns have the same number of elements (10950) and all my rows as well (10), but when i run this i get the error:

Error in Ops.data.frame(xyz, xyy[!rowSums(xyy == -999), ]) : 
  ‘==’ only defined for equally-sized data frames

this is the head of my dataframe:

3035 1989  4 25  0   32.8   25.8  0  0 26.2   0
3036 1989  4 26  0 -999.0   23.6  0  0  1.0   0
3037 1989  4 27  0   32.4   18.6  0  0  0.0   0
3038 1989  4 28  0   31.8   19.6  0  0  0.0   0
3039 1989  4 29  0   33.2   19.2  0  0  0.0   0
3040 1989  4 30  0   32.4   19.6  0  0  0.0   0
3041 1989  5  1  0   33.0   19.8  0  0  0.0   0
3042 1989  5  2  0   32.6   18.8  0  0  0.0   0
3043 1989  5  3  0   32.8   19.2  0  0  0.0   0
3044 1989  5  4  0 -999.0 -999.0  0  0  1.0   0

help!


Solution

  • This filters all rows containing -999.

    library(dplyr)
    df %>%
      filter_all(all_vars(. !=-999))
    

    Where df is a data frame. Make sure there is no NA in the rows otherwise change the filter_all to filter_all(all_vars(. !=-999 | is.na(.))).