Capturing the Total of Possible Non Missing Value ( Loops + Data frame )

hello dear friends ,

If I have the following vector for example :
d <- c(1:4)
i want to generate a data frame contain the total of all Possible non missing value which may occur ,
secnario 1 : no missing value
Scenario 2 : 1 missing value
Scenario 3 : 2 missing value
Scenario 4: 3 missing value
and so on ..

basically i want to generate similar to this data.frame .
I found it very challenging for me even with loops i couldn't figure out the mechanism how to do it . the table which i have contain more than 60 columns :blush:

Thank you

Are you familiar with complete.cases and colSums?

yes dear ,,

the main challenge for me is to create the data frame with all possible outcome .. not capturing the total of it . it was my fault I put the total in it as well .

any suggestion .

thank you

Do you mean the possible combinations of NA that could be present or the actual that are present?


d <- c(1:4)
r1 <-lapply(c(0,seq_along(d)) ,
       \(x){
         combn(d,x,simplify = FALSE)
       }) |> flatten()
str(r1)

here r1 is a list, and each entry of the list gives positions at which NA might be found. given d

List of 16
 $ : int(0) 
 $ : int 1
 $ : int 2
 $ : int 3
 $ : int 4
 $ : int [1:2] 1 2
 $ : int [1:2] 1 3
 $ : int [1:2] 1 4
 $ : int [1:2] 2 3
 $ : int [1:2] 2 4
 $ : int [1:2] 3 4
 $ : int [1:3] 1 2 3
 $ : int [1:3] 1 2 4
 $ : int [1:3] 1 3 4
 $ : int [1:3] 2 3 4
 $ : int [1:4] 1 2 3 4

therefore we can do

nd <- d
names(nd) <- d

map_dfr(r1,\(x){
  local_d <- nd
  local_d[x] <- NA
  local_d
})
# A tibble: 16 × 4
     `1`   `2`   `3`   `4`
   <int> <int> <int> <int>
 1     1     2     3     4
 2    NA     2     3     4
 3     1    NA     3     4
 4     1     2    NA     4
 5     1     2     3    NA
 6    NA    NA     3     4
 7    NA     2    NA     4
 8    NA     2     3    NA
 9     1    NA    NA     4
10     1    NA     3    NA
11     1     2    NA    NA
12    NA    NA    NA     4
13    NA    NA     3    NA
14    NA     2    NA    NA
15     1    NA    NA    NA
16    NA    NA    NA    NA
1 Like

that was amazing bro .. thanks a lot ..

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.