1

This is almost certainly a duplicate question, but I can't find an an answer anywhere on SO. Most of the other similar questions relate to subsetting from one column, rather than an entire dataframe.

I have a dataframe:

test = data.frame(
'A' = c(.31562, .48845, .27828, -999),
'B' = c(.5674, 5.7892, .4687, .1345),
'C' = c(-999, .3145, .0641, -999))

I want to drop rows where any column contains -999, so that my dataframe will look like this:

           A      B         C
2    0.48845 5.7892    0.3145
3    0.27828 0.4687    0.0641

I am sure there is an easy way to do this with the subset() function, or apply(), but I just can't figure it out.

I tried this:

test[apply(test, MARGIN = 1, FUN = function(x) {-999 != x}), ]

But it returns:

              A      B         C
1       0.31562 0.5674 -999.0000
2       0.48845 5.7892    0.3145
4    -999.00000 0.1345 -999.0000
NA           NA     NA        NA
NA.1         NA     NA        NA
NA.2         NA     NA        NA
NA.3         NA     NA        NA
NA.4         NA     NA        NA
NA.5         NA     NA        NA
Mark
  • 639
  • 1
  • 6
  • 15
ale19
  • 1,327
  • 7
  • 23
  • 38
  • 3
    `test[!apply(test, 1, function(r) any(r == -999)),]` – bouncyball Jun 19 '17 at 16:27
  • 1
    you could deal with this at an earlier stage: when reading in set `na.strings="-999"` . Then the usual tools for removing rows with missing data can be used. – user20650 Jun 19 '17 at 16:30
  • With `rowSums`, you could do `test[rowSums(test == -999) == 0,]`, but using `na.strings` in the read argument is a better choice. – lmo Jun 19 '17 at 16:35

2 Answers2

3

Use arr.ind with which to obtain the rows where -999 is present (which(test == -999, arr.ind = TRUE)[,1])and remove those rows.

test[-unique(which(test == -999, arr.ind = TRUE)[,1]),]
#        A      B      C
#2 0.48845 5.7892 0.3145
#3 0.27828 0.4687 0.0641
d.b
  • 32,245
  • 6
  • 36
  • 77
1

We can use Reduce

test[!Reduce(`|`, lapply(test, `==`, -999)),]
#        A      B      C
#2 0.48845 5.7892 0.3145
#3 0.27828 0.4687 0.0641
akrun
  • 874,273
  • 37
  • 540
  • 662