Set a new NA variable if any other NA parameter

I want to add a new variable (N_notNAs) to my data frame, which determines if any of the other variables are NA.

x   y   z   N_notNAs
2   3   NA  NA
NA  1   3   NA
2   3   5   1
4   4   3   1
+4
source share
6 answers

Not sure why this is your desired result, but a possible way to achieve this is to sum NAper line and put its power NA- this way NA ^ 0 will return 1, and everything else will become NA

NA^rowSums(is.na(df))
# [1] NA NA  1  1
+11
source

@ David Arenburg's decision is very elegant, but here are a few more.

4 , df , 3 . .

ifelse , .

, @alexis_laz, apply(...) !complete.cases(df) ifelse(complete.cases(df), 1, NA), c(NA, 1)[complete.cases(df) + 1] match(complete.cases(df), TRUE).

rowSums(0*df) + 1

max.col(0*df) + 1

do.call(pmin, 0*df) + 1

do.call(pmax, 0*df) + 1

ifelse(apply(df, 1, anyNA), NA, 1)

c(NA, 1)[apply(df, 1, anyNA) + 1]

match(apply(df, 1, anyNA), FALSE)

, NA 1. TRUE/FALSE , apply(df, 1, anyNA) .

+6

: TRUE/FALSE , NA 1 .

For the sake of information, here are the standards on my computer for 3 columns of data.frame with 1e6 rows:

Unit: milliseconds
               expr        min         lq       mean     median         uq        max neval    cld
     alexis_laz(df)   12.87138   13.30044   15.46142   13.49258   13.80019   29.59228    10 a     
          akrun(df)   46.06203   48.31564   49.82198   49.94947   51.05219   53.91161    10 ab    
 GGrothendieck3(df)   55.42513   56.59798   69.37274   59.16803   64.44442  155.62797    10 ab    
 GGrothendieck4(df)   54.88489   58.08043   69.54111   58.63820   65.36838  149.21380    10 ab    
 GGrothendieck2(df)   60.26961   62.37184   97.93301   69.80034  158.39302  193.03562    10  bc   
          By989(df)  115.30531  118.81843  133.44343  123.17356  130.36815  223.22601    10   c   
 GGrothendieck1(df)  123.99504  128.61030  140.62055  132.31073  137.83856  220.33666    10   c   
          David(df)  131.42639  131.66415  143.03384  133.50082  136.29453  225.17487    10   c   
 GGrothendieck7(df) 1100.69319 1109.60500 1147.25668 1142.83955 1156.37090 1270.32547    10    d  
 GGrothendieck6(df) 1060.97719 1124.85486 1148.54833 1140.91949 1170.62952 1247.80220    10    d  
 GGrothendieck5(df) 1218.79235 1251.03109 1287.47851 1285.20543 1311.82753 1364.89158    10     e 
   PaulHiemstra(df) 1436.31149 1461.14340 1511.42476 1502.34413 1552.09517 1608.22418    10      f

And for df of 1e5 columns with 1e3 rows:

Unit: milliseconds
               expr        min         lq       mean     median         uq        max neval  cld
     alexis_laz(df)   356.1987   360.8647   366.2464   364.4488   368.9666   391.5828    10 a   
          David(df)  1387.1657  1415.7325  1530.0748  1436.9192  1542.1830  1968.9455    10 a   
          akrun(df)  1773.5728  1800.9288  1880.9201  1868.3143  1965.7862  2018.0870    10 a   
 GGrothendieck5(df)  4891.3247  5385.9903  8206.9116  9065.2893  9890.5795 10284.7369    10  b  
 GGrothendieck6(df)  5034.4408  9089.9334  9099.5746  9785.7042 10221.1537 11905.3997    10  b  
 GGrothendieck7(df)  5142.7372  9635.2558  9711.4691  9861.5164 10524.7317 11651.6198    10  b  
   PaulHiemstra(df)  5326.8807  9959.3951 10079.1672 10175.4814 11048.6218 12659.1130    10  b  
          By989(df)  9941.5236 10015.6652 10090.2076 10067.7127 10123.5885 10300.4110    10  b  
 GGrothendieck2(df) 25715.5451 25840.3138 26686.3386 26453.6770 26982.5627 29689.6019    10   c 
 GGrothendieck3(df) 26065.7005 26343.5734 27112.4387 26470.7166 27267.7166 31374.5133    10   c 
 GGrothendieck4(df) 25911.6476 26179.3999 27121.3442 26361.2242 27335.2762 31941.6339    10   c 
 GGrothendieck1(df) 34979.3212 35162.3589 36254.1681 35685.4975 36470.3027 41130.0531    10    d

Source:

David <-function(df) {
  NA^rowSums(is.na(df))
}

By989 <- function(df) {
  rowSums(df) & rowSums(df, na.rm = T)
}

PaulHiemstra <- function(df) {
  ifelse(apply(is.na(df), 1, any), NA, 1)
}

akrun <- function(df) {
  NA^Reduce(`|`, lapply(df, is.na))
}

GGrothendieck1 <- function(df) {
  rowSums(0*df) + 1
}

GGrothendieck2 <- function(df) {
  max.col(0*df) + 1
}

GGrothendieck3 <- function(df) {
  do.call(pmin, 0*df) + 1
}

GGrothendieck4 <- function(df) {
  do.call(pmax, 0*df) + 1
}

GGrothendieck5 <- function(df) {
  ifelse(apply(df, 1, anyNA), NA, 1)
}

GGrothendieck6 <- function(df) {
  c(NA, 1)[apply(df, 1, anyNA) + 1]
}

GGrothendieck7 <- function(df) {
  match(apply(df, 1, anyNA), FALSE)
}

alexis_laz <- function(df) {
  complete.cases(df)
}

set.seed(5)
n<-function(x) sample(c(1:5,NA),1e6,replace=TRUE) 
df<-data.frame(A=n(),B=n(),C=n())
results<-microbenchmark(David(df),
               By989(df),
               PaulHiemstra(df),
               akrun(df),
               GGrothendieck1(df),
               GGrothendieck2(df),
               GGrothendieck3(df),
               GGrothendieck4(df),
               GGrothendieck5(df),
               GGrothendieck6(df),
               GGrothendieck7(df),
               alexis_laz(df),
               times=10)
print(results,order="mean")
+5
source

Another variant: Reduce

NA^Reduce(`|`, lapply(df1, is.na))
#[1] NA NA  1  1
+3
source

I like this option over @davids because it is a bit more expressive, i.e. the code shows more what has been done.

ifelse(apply(is.na(df), 1, any), NA, 1)

For example, there is no need to remember that NA ^ 0 is equal to one.

+2
source

OR

rowSums(df) & rowSums(df, na.rm = T)
#[1]   NA   NA TRUE TRUE
+2
source

Source: https://habr.com/ru/post/1661584/


All Articles