I am trying to randomly sample from a matrix (*b* below) but I want the resulting matrix of samples to have a proportion of zeros in each column equal to that of another matrix (*a* below). I am trying to use `sample()` function to do this but I'm not having much joy. Some reproducible code is below which will hopefully explain my problem:

---

EDIT: Just to mention I don't want any rows augmented or edited but instead the random sampling from `b` to be done such that the resulting matrix; `b_sample` will approximately have an equal distribution of zeros to `a`

---

```
set.seed(1234)
# matrix a is the matrix that holds the distribution of zeros I want to match
a <- matrix(as.integer(rexp(200, rate=.1)), ncol=20)
# matrix b is the matrix to be sampled from 
b <- matrix(as.integer(rexp(2000, rate=.1)), ncol=20)

```

a looks like:

```
     [,1] [,2] [,3] [,4] [,5]
[1,]    6    0    6    1   22
[2,]   19    6    0   23   19
[3,]    8   22    8    5    0
[4,]   24   17   28    3    0

```

b looks like:

```
      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    1   10    5    9
 [2,]   26    1    3    2    2
 [3,]    4    8    3    0    0
 [4,]    2   10   35    3   11
 [5,]    1    3   16    0    6
 [6,]    2    4    2   16    2
 [7,]    3   18   13    6   17
 [8,]    0    2    9    0   13
 [9,]    2   15    6   27   30
[10,]    1    2    7    9   15
[11,]   13    0    5    1    2
[12,]   18   12    9   27   33
[13,]    0   20    3   18    1
[14,]    5    7    7   16    4
[15,]    5    6    4    5    2
[16,]    0    7    5   10    7
[17,]    3   20    5   14   34
[18,]   28    0   10    5    8
[19,]   33    0    2    6   13
[20,]    7   28    0   11    8

```

I extract the distribution of zeros in each column of `a` to use in the sampling

```
dist<-apply(a,2, function(x) sum(x!=0)/length(x)) 
dist
[1] 1.00 0.75 0.75 1.00 0.50

```

I then go on to try and sample from `b` to hold the same number of rows as a

```
b_sample<-b[sample(x=nrow(b),
                   size=4,
                   replace=F
                   )
            ,]

```

This will work but I want the `b_sample` to to have the same proportion of zeros in each column as `a`. I have tried to do this

```
b_sample<-b[sample(x=nrow(b),
                   size=4,
                   replace=F,
                   prob=dist
                   )
            ,]

```

but I get an error:

```
Error in sample.int(x, size, replace, prob) : 
  incorrect number of probabilities

```

I am not sure if I have the format wrong to do this or is the `sample()` function not the correction function at all to use. Any help would be greatly appreciated!

---

EDIT 2: Update below

---

I have found a way to sample from `b` and keep the proportions of zeros in the resulting `b_sample` the same as the original `b`. This is not what I am trying to get, I want the proportions to be equal to that in `a` but it might give a better idea of what I want to do. See below for a development on the above example

First I made `b` into a dataframe and indexed the rows to make use of `dplyr` and `groupby()`

```
b_df<-as.data.frame(b)
b_df <- b_df %>%
  mutate(n = row_number()) %>% #create row number
  select(n, everything()) # put row number at the front of the dataset
b_df
    n V1 V2 V3 V4 V5
1   1 19  1 29  2  9
2   2  7 20  1  3  9
3   3  3 25  8  9 22
4   4  9  0 20  9  0
5   5  2 12 14  4  2
6   6 10 22  9  1  9
7   7  0  9 16  1  4
8   8  3  3 14 23  2
9   9  7  0  7  1  0
10 10  9  0 26  2  6
11 11  4 19  0  2  6
12 12  0  2  1  7  4
13 13 16 16 25  2  3
14 14  0  1  1  7  9
15 15  8 14  0  9  5
16 16  0 14  9  5  0
17 17 43 27 14  1  4
18 18  9  0 13  4  9
19 19  0  8  3  9 13
20 20 34 36  1  7 20

```

I then create a binary dataframe to indicate if each cell has a 0 or a value

```
b_df_0[,-1]<-as.data.frame(lapply(b_df[,-1],function(x) x==0))
b_df_0
    n    V1    V2    V3    V4    V5
1   1 FALSE FALSE FALSE FALSE FALSE
2   2 FALSE FALSE FALSE FALSE FALSE
3   3 FALSE FALSE FALSE FALSE FALSE
4   4 FALSE  TRUE FALSE FALSE  TRUE
5   5 FALSE FALSE FALSE FALSE FALSE
6   6 FALSE FALSE FALSE FALSE FALSE
7   7  TRUE FALSE FALSE FALSE FALSE
8   8 FALSE FALSE FALSE FALSE FALSE
9   9 FALSE  TRUE FALSE FALSE  TRUE
10 10 FALSE  TRUE FALSE FALSE FALSE
11 11 FALSE FALSE  TRUE FALSE FALSE
12 12  TRUE FALSE FALSE FALSE FALSE
13 13 FALSE FALSE FALSE FALSE FALSE
14 14  TRUE FALSE FALSE FALSE FALSE
15 15 FALSE FALSE  TRUE FALSE FALSE
16 16  TRUE FALSE FALSE FALSE  TRUE
17 17 FALSE FALSE FALSE FALSE FALSE
18 18 FALSE  TRUE FALSE FALSE FALSE
19 19  TRUE FALSE FALSE FALSE FALSE
20 20 FALSE FALSE FALSE FALSE FALSE

```

I then use `group_by` and `sample_frac` from `dplyr` to sample from `b` to equal the number of samples in `a`. 

```
proportion <- nrow(a)/nrow(b)
sample <- b_df_0 %>%
  group_by(V1,V2,V3,V4,V5) %>% #any number of variables you wish to partition by proportionally
  sample_frac(proportion) # proportion of the original df you wish to sample

b_df[b_df$n %in% sample$n,]
#The above approach would work if you can get a proportions = b proportions
    n V1 V2 V3 V4 V5
2   2  7 20  1  3  9
19 19  0  8  3  9 13
20 20 34 36  1  7 20

```

This approach is not what I want however as the proportions are based on `b` when I want them to be based on `a`. Any help on how to do this would be amazing! Thanks!