Reputation: 1367
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
Upvotes: 1
Views: 249
Reputation: 887213
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
Upvotes: 1
Reputation: 32548
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
Upvotes: 3