Fastest way to detect if vector has at least 1 NA?

后端 未结 6 1117
南旧
南旧 2020-12-23 14:50

What is the fastest way to detect if a vector has at least 1 NA in R? I\'ve been using:

sum( is.na( data ) ) > 0

But that

6条回答
  •  一个人的身影
    2020-12-23 15:18

    Here are some actual times from my (slow) machine for some of the various methods discussed so far:

    x <- runif(1e7)
    x[1e4] <- NA
    
    system.time(sum(is.na(x)) > 0)
    > system.time(sum(is.na(x)) > 0)
       user  system elapsed 
      0.065   0.001   0.065 
    
    system.time(any(is.na(x)))  
    > system.time(any(is.na(x)))
       user  system elapsed 
      0.035   0.000   0.034
    
    system.time(match(NA,x)) 
    > system.time(match(NA,x))
      user  system elapsed 
     1.824   0.112   1.918
    
    system.time(NA %in% x) 
    > system.time(NA %in% x)
      user  system elapsed 
     1.828   0.115   1.925 
    
    system.time(which(is.na(x) == TRUE))
    > system.time(which(is.na(x) == TRUE))
      user  system elapsed 
     0.099   0.029   0.127
    

    It's not surprising that match and %in% are similar, since %in% is implemented using match.

提交回复
热议问题