rdataframerandomconstraintsrowsum

Generating random dataframes (5 rows 9 cols) with each rowsum should be 9


I'm trying to create 10 or more of pseudo dataframes. The data frame dim should be 9 columns with 5 rows(Mon, Tue,Wed, Thur, Fri), and each rowsum should be 9. like below.

        Factor1 Factor2 Factor3 Factor4 Factor5 Factor6 Factor7 Factor8 Factor9
Mon       2       1       0       2       0       0       1       1       2
Tue       1       1       1       1       0       0       2       1       2
Wed       2       1       0       2       1       1       1       1       0
Thu       0       0       1       1       3       0       2       2       0
Fri       1       0       0       1       1       0       2       2       2

How can I generate multiple dataframes that meet the condition, please?


Solution

  • Here is a function that will generate random matrices to your specifications.

    GenDF = function() {
        M = matrix(0, nrow=5, ncol=9)
        for(i in 1:5) {
            S = sample(9,9,replace=T)
            for(j in S) { M[i,j] = M[i,j] + 1 }
        }
        rownames(M) = c('Mon', 'Tue', 'Wed', 'Thu','Fri')
        colnames(M) = paste('Factor', 1:9, sep='')
        as.data.frame(M)
    }
    
    GenDF()
        Factor1 Factor2 Factor3 Factor4 Factor5 Factor6 Factor7 Factor8 Factor9
    Mon       3       3       1       1       0       0       0       0       1
    Tue       3       1       0       1       0       2       0       2       0
    Wed       1       0       1       1       0       1       2       1       2
    Thu       1       2       0       1       1       1       3       0       0
    Fri       0       1       1       2       2       0       0       3       0
    

    To elaborate on why the rows sum to one: The line S = sample(9,9,replace=T) will choose nine numbers between one and nine with replacement. The idea is that each one of the selected numbers represents one of the nine items to be distributed across the nine columns. The number selected tells you which column it will go into. Since the selection is being made with replacement, sometimes a column gets more than one of the nine items.