行と列の繰り返しを使わずに行列をランダム化するために大きな助けを得た後、変更に関するもう1つの質問があります。R - 列と行に繰り返されない行列の固定値にランダム値を追加します。
まず、何が行われたかで始めます。ランダム化された行と列が重複しない(行と列の)賢明な行列が必要でした。ここで再び大きな助けのおかげで、これは私が使用して終了jdobres(https://stackoverflow.com/users/6436545/jdobres)によってコードです:
# number of rows and columns
n <- 10
# create ordered rows and columns
ordered.by.row <- matrix(1:n, n, n)
ordered.by.col <- matrix(1:n, n, n, byrow = T)
# offset the rows and columns relative to each other.
# no row or column has a repeated value, but the values are still ordered
offset <- (ordered.by.row + ordered.by.col) %% n + 1
# shuffle the columns, then shuffle the rows, this produces a randomized matrix
# 'shuffle.row' is the final, randomized matrix
set.seed(1222) # change this to 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 and columns
ordered.by.row <- matrix(1:n, n, n)
ordered.by.col <- matrix(1:n, n, n, byrow = T)
# offset the rows and columns relative to each other.
# no row or column has a repeated value, but the values are still ordered
offset <- (ordered.by.row + ordered.by.col) %% n + 1
# shuffle the columns, then shuffle the rows, this produces a randomized matrix
# 'shuffle.row' is the final, randomized matrix
set.seed(1222) # change this to 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
私は今、私は私の最初の行(行1,2,3を言うことができます)をしたいという問題があるしかし、特定の順序で固定されています。それにもかかわらず、私は新たな行4〜10を再度ランダム化し、重複しないようにする必要があります(行1-3に関する重複もありません)!
[,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
しかし、最初の行を変更せずに値(任意の行または列の)私は重複し、ランダム化ではなく、したい次の7行の
... :だから例えば私はこのような3行を持っています
アイデア私はシャッフルから私が(行1-3)滞在したいものを除外する方法を見つけることができませんが、残りの行と列に重複はまだありません...
あなたの助けがまた大いに感謝されるでしょう!
EDIT:あなたは助けるために
はMoody_Mudskipper非常をありがとうございます!解決策は、私の(偽の)データを使用してどのように見えるこの:
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 !!
もCOLUMNS UNIQUE
行インデックスを指定して、関数が行1〜3をシャッフルしないようにすることができます。 'matrix [4:nrow(行列)、]' –
ありがとう!しかし、私はこの仕様をどこで実装しますか?オフセットの前に行が固定されている必要があります...上記の問題の詳細な説明を参照してください。 – shampoo