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
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 .
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