R - Add random values to fixed values in matrix that are not repeated over column and row -


after getting great here randomize matrix without repetitions in rows , columns (fixed values not repeated on column , row) have question concerning modification.

so, first start has been done: wanted matrix randomized rows , columns without duplications in each (row wise , column) wise. again great here code jdobres (https://stackoverflow.com/users/6436545/jdobres) ended using:

# number of rows , columns n <- 10  # create ordered rows , columns ordered.by.row <- matrix(1:n, n, n) ordered.by.col <- matrix(1:n, n, n, byrow = t)  # offset rows , columns relative each other. # no row or column has repeated value, values still ordered offset <- (ordered.by.row + ordered.by.col) %% n + 1  # shuffle columns, shuffle rows, produces randomized matrix # 'shuffle.row' final, randomized matrix set.seed(1222) # change change randomization shuffle.col <- offset[,sample(1:n, n, replace = f)] shuffle.row <- shuffle.col[sample(1:n, n, replace = f), ]  # verify solution any(apply(shuffle.row, 1, function(r)any(duplicated(r)))) # false any(apply(shuffle.row, 2, function(r)any(duplicated(r)))) # false   > # create ordered rows , columns ordered.by.row <- matrix(1:n, n, n) ordered.by.col <- matrix(1:n, n, n, byrow = t)  # offset rows , columns relative each other. # no row or column has repeated value, values still ordered offset <- (ordered.by.row + ordered.by.col) %% n + 1  # shuffle columns, shuffle rows, produces randomized matrix # 'shuffle.row' final, randomized matrix set.seed(1222) # change change randomization shuffle.col <- offset[,sample(1:n, n, replace = f)] shuffle.row <- shuffle.col[sample(1:n, n, replace = f), ]  # verify solution any(apply(shuffle.row, 1, function(r)any(duplicated(r)))) # false any(apply(shuffle.row, 2, function(r)any(duplicated(r)))) # false        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,]    1   10    6    9    2    8    3    5    7     4  [2,]    3    2    8    1    4   10    5    7    9     6  [3,]    7    6    2    5    8    4    9    1    3    10  [4,]    9    8    4    7   10    6    1    3    5     2  [5,]   10    9    5    8    1    7    2    4    6     3  [6,]    2    1    7   10    3    9    4    6    8     5  [7,]    8    7    3    6    9    5   10    2    4     1  [8,]    6    5    1    4    7    3    8   10    2     9  [9,]    5    4   10    3    6    2    7    9    1     8 [10,]    4    3    9    2    5    1    6    8   10     7 

however have issue want first rows (lets row 1,2,3) fixed in specific order. nevertheless need new rows 4 10 again randomized , without duplicates (also no duplicates concerning rows 1-3)!

so example have 3 rows this:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]  [1,]    8    7    3    6    9    5   10    2    4     1  [2,]    6    5    1    4    7    3    8   10    2     9  [3,]    5    4   10    3    6    2    7    9    1     8 

but next 7 rows want randomized not duplicated (in row or column) values without changing first rows...

any ideas? cannot figure out how exclude ones want stay (row 1-3) shuffeling still have no duplicates in rest of rows , columns...

your again appreciated!

edit:

thank moody_mudskipper help! solution looks using (fake) data:

mat<-as.matrix(first.rows) nkeep <- 3 mat_shuffled <- mat[c(1:nkeep,sample((nkeep+1):nrow(mat),replace=false)),]      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] row1    1    4    7    6    5    3    2    8    9    10 row2   10    7    3    2    1    4    5    9    8     6 row3    9    2    4    3    5    7   10    1    6     5        10    7    9    6    8    2    5    4    3     1         1   10    8    4    7    3    5    2    6     9        10    6    4    1    8    3    7    2    5     9         2    5    7    8    9    6    1    3    4    10         2    1   10    4    8    9    3    6    5     7         8    5    3    2    4    1   10    7    6     9         6    1    5    4    2   10    3    8    7     9 

thanky you!!

see constructing randomised matrix no duplicates fixed partial input solution fernando keeps values in columns unique

would work ?

mat <- matrix(1:100,10) nkeep <- 3 mat_shuffled <- mat[c(1:nkeep,sample((nkeep+1):nrow(mat),replace=false)),] #      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] # [1,]    1   11   21   31   41   51   61   71   81    91 # [2,]    2   12   22   32   42   52   62   72   82    92 # [3,]    3   13   23   33   43   53   63   73   83    93 # [4,]    6   16   26   36   46   56   66   76   86    96 # [5,]   10   20   30   40   50   60   70   80   90   100 # [6,]    7   17   27   37   47   57   67   77   87    97 # [7,]    4   14   24   34   44   54   64   74   84    94 # [8,]    5   15   25   35   45   55   65   75   85    95 # [9,]    9   19   29   39   49   59   69   79   89    99 # [10,]   8   18   28   38   48   58   68   78   88    98 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -