The R Project SVN R

Rev

Rev 62675 | Rev 65263 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 62675 Rev 63206
Line 110... Line 110...
110
(xx <- c(0:4))
110
(xx <- c(0:4))
111
is.na(xx) <- c(2, 4)
111
is.na(xx) <- c(2, 4)
112
xx                     #> 0 NA  2 NA  4
112
xx                     #> 0 NA  2 NA  4
113
anyNA(xx) # TRUE
113
anyNA(xx) # TRUE
114
 
114
 
-
 
115
\donttest{
115
## Measure speed difference in an extreme case:
116
## Measure speed difference in an extreme case:
116
if(require("microbenchmark")) {
117
if(require("microbenchmark")) { # does not work reliably on all platforms
117
  x <- c(NaN, 1:10000)
118
  x <- c(NaN, 1:10000)
118
  print(microbenchmark(any(is.na(x)), anyNA(x)))
119
  print(microbenchmark(any(is.na(x)), anyNA(x)))
119
} else { ## much less accurate
120
} else { ## much less accurate
120
  x <- c(NaN, 1e6)
121
  x <- c(NaN, 1e6)
121
  nSim <- 2^13
122
  nSim <- 2^13
122
  print(rbind(is.na = system.time(replicate(nSim, any(is.na(x)))),
123
  print(rbind(is.na = system.time(replicate(nSim, any(is.na(x)))),
123
              anyNA = system.time(replicate(nSim, anyNA(x)))))
124
              anyNA = system.time(replicate(nSim, anyNA(x)))))
124
}
125
}
-
 
126
}
125
 
127
 
126
## anyNA() works recursively with list()s:
128
## anyNA() works recursively with list()s:
127
LL <- list(1:5, c(NA, 5:8), c("A","NA"), c("a", NA_character_))
129
LL <- list(1:5, c(NA, 5:8), c("A","NA"), c("a", NA_character_))
128
L2 <- LL[c(1,3)]
130
L2 <- LL[c(1,3)]
129
sapply(LL, anyNA); anyNA(LL)
131
sapply(LL, anyNA); anyNA(LL)
Line 131... Line 133...
131
 
133
 
132
## ... lists, and hence data frames, too; no method needed:
134
## ... lists, and hence data frames, too; no method needed:
133
dN <- dd <- USJudgeRatings; dN[3,6] <- NA
135
dN <- dd <- USJudgeRatings; dN[3,6] <- NA
134
anyNA(dd) # FALSE
136
anyNA(dd) # FALSE
135
anyNA(dN) # TRUE
137
anyNA(dN) # TRUE
136
\dontshow{
-
 
137
stopifnot(anyNA(x), anyNA(xx),
-
 
138
          anyNA(LL), !anyNA(L2),
-
 
139
          anyNA(dN), !anyNA(dd), !any(is.na(dd)),
-
 
140
          all(c(3,6) == which(is.na(dN), arr.ind=TRUE)))
-
 
141
}
-
 
142
}
138
}
143
\keyword{NA}
139
\keyword{NA}
144
\keyword{logic}
140
\keyword{logic}
145
\keyword{manip}
141
\keyword{manip}