Search code examples
rrandomsample

Random values (0 and 1) with condition on each row and each column


Hi I am trying to create a dataframe of 600 rows and 18 columns in R BUT:

-each row has to have only three 1's randomly in the 18 columns (for example column A,E,F with 1 and the rest with 0's) -the sum of each column has to be equal to 100

I am really stuck with this problem :(


Solution

  • You can do that with the RaschSampler package.

    It implements a MCMC sampler for binary (0/1) matrices with fixed margins. For a MCMC sampler, an initial value is required.

    # initial matrix
    M0 <- matrix(0, nrow=600, ncol=18)
    M0[1:100,1:3] <- M0[101:200,4:6] <- M0[201:300,7:9] <- 
      M0[301:400,10:12] <- M0[401:500,13:15] <- M0[501:600,16:18] <- 1
    # check margins
    all(colSums(M0)==100)
    all(rowSums(M0)==3)
    
    # MCMCM sampler
    library(RaschSampler)
    sampling <- rsampler(M0)
    
    # extract a sampled matrix (not the first one: this is M0)
    M <- rsextrmat(sampling, mat.no = 2) 
    # check margins
    all(colSums(M)==100)
    all(rowSums(M)==3)
    

    This works:

    > # check margins
    > all(colSums(M)==100)
    [1] TRUE
    > all(rowSums(M)==3)
    [1] TRUE