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

二次信任 提交于 2020-01-15 08:24:14

问题


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 :(


回答1:


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



回答2:


This is a partial answer, the sums of columns are 100 but the columns are not random:

m <- matrix(nrow = 600, ncol = 18)
for (i in 0:5) {
    a <- ((100 * i) + 1) : ((i + 1) * 100)
    b <- ((3 * i) + 1) : ((i + 1) * 3)
    m[a, b] <- 1
}

m <- m[sample(1:600, 600), ]


来源:https://stackoverflow.com/questions/51378594/random-values-0-and-1-with-condition-on-each-row-and-each-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!