Reputation: 269
I am trying to get all rows where specific values (here RATIO1 and RATIO 2) are NaN an keep them.
consider a data frame like
data.frame(ID=c(1,2,4,6,7),RATIO1=c(NaN,NaN,0.2,9.5,6),
RATIO2=c(NaN,NaN,0.5,NaN,6), RATIO3=c(0.3,0.2,4,NaN,5))
ID RATIO1 RATIO2 RATIO3
1 1 NaN NaN 0.3
2 2 NaN NaN 0.2
3 4 0.2 0.5 4
4 6 9.5 NaN NaN
5 7 6 6 5
I want it to look like this
ID RATIO1 RATIO2 RATIO3
1 1 NaN NaN 0.3
2 2 NaN NaN 0.2
I could make it using is.na() or complete.cases () <- this will delete the rows.
Thx
Upvotes: 3
Views: 12004
Reputation: 162371
Here's one possibility, using apply()
to examine the rows one at a time and determine whether they are fully composed of NaN
s:
df[apply(df[2:3], 1, function(X) all(is.nan(X))),]
# ID RATIO1 RATIO2 RATIO3
# 1 1 NaN NaN 0.3
# 2 2 NaN NaN 0.2
Upvotes: 6
Reputation: 7561
Enhanced solution based on this from Josh O'Briens.
df[rowSums(is.nan(as.matrix(df[2:3])))>1,]
#
# as.matrix because is.nan requires matrix (but is.na doesn't)
# rowSums for speed gain
#
Upvotes: 1
Reputation: 1595
You could use this:
df <- data.frame(ID=c(1,2,4,6,7),RATIO1=c(NaN,NaN,0.2,9.5,6),
RATIO2=c(NaN,NaN,0.5,NaN,6), RATIO3=c(0.3,0.2,4,NaN,5))
df[is.nan(df$RATIO1) & is.nan(df$RATIO2),]
ID RATIO1 RATIO2 RATIO3
1 1 NaN NaN 0.3
2 2 NaN NaN 0.2
Upvotes: 0
Reputation: 173617
Assuming you really are dealing with NaN
and not some arbitrary character value, try something like this:
dat <- data.frame(ID=c(1,2,4,6,7),RATIO1=c(NaN,NaN,0.2,9.5,6), RATIO2=c(NaN,NaN,0.5,NaN,6), RATIO3=c(0.3,0.2,4,NaN,5))
> dat[is.nan(dat$RATIO1) & is.nan(dat$RATIO2),]
ID RATIO1 RATIO2 RATIO3
1 1 NaN NaN 0.3
2 2 NaN NaN 0.2
is.finite
might also prove useful if you're doing this sort of thing often.
Upvotes: 6