3

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

4

4 回答 4

6

Here's one possibility, using apply() to examine the rows one at a time and determine whether they are fully composed of NaNs:

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
于 2013-01-03T23:29:13.033 回答
6

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.

于 2013-01-03T23:29:27.337 回答
1

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
#
于 2013-01-04T08:31:58.297 回答
0

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
于 2013-01-03T23:42:17.550 回答