I have a number of subarrays, say 2 (for simplicity), each with the same number of rows and columns. Each spot in the subarrays is occupied by a number in [1, 10].
What I would like to do is move rows randomly between subarrays according to some rate of movement m = [0, 1]. m = 0 corresponds to no movement, while m = 1 means that any rows across all subarrays can be moved.
I take inspiration from:
How to swap a number of the values between 2 rows in R
but my problem is a bit different than this. I do know that sample() would be needed here.
Is there an easy way to go about accomplishing this?
This doesn't do it, but I believe I'm on the right track anyway.
m <- 0.2
a <- array(dim = c(5, 5, 2)) # 5 rows, 5 columns, 2 subarrays
res <- rep(sample(nrow(a), size = ceiling(nrow(a)*m), replace = FALSE)) # sample 20% of rows from array a.
Any assistance is appreciated.
It is significantly easier if you can use a
matrix
(2-dimarray
).(I'm going to set the seed again to something that conveniently gives me something "interesting" to show.)
The first randomness,
runif
, is compared againstm
and generates the indices that may change. The second randomness,sample
below, takes those indices and possibly reorders them. (In this case, it reorders "1,4,7" to "4,1,7", meaning the third of the rows-that-may-change will be left unchanged.)Note that this is probabilistic, which means a probability of 0.2 does not guarantee you 20% (or even any) of the rows will be swapped.
(Since I'm guessing you'd really like to preserve your 3-dim (or even n-dim)
array
, you might be able to useaperm
to transfer betweenarray <--> matrix
.)EDIT 1
As an alternative to a probabilitic use of
runif
, you can use:to get closer to your goal of "20%". Since
d[1]*m
will often not be an integer,head
silently truncates/floors the number, so you'll get the price-is-right winner: closest to but not over your desired percentage.EDIT 2
A reversible method for transforming an n-dimensional array into a matrix and back again. Caveat: though the logic appears solid, my testing has only included a couple arrays.
The reversal uses the
"origdim"
attribute if still present; this will work as long as your modifications to the matrix do not clear its attributes. (Simple row-swapping does not.)(These two functions should probably do some more error-checks, such as
is.null(d)
.)A sample run:
Quick show:
The transformation:
Proof of reversability:
EDIT 3, "WRAP UP of all code"
Creating fake data:
The random-swapping of rows. I'm using 50% here.
(Note that I pre-made
ind1
andind2
here, mostly to see what was going on internally. You can replacem[ind2,]
withm[sample(ind1),]
for the same effect.)BTW: if we had instead used a seed of 2, we would notice that 2 rows are not swapped:
Because of this, I chose a seed of 3 for demonstration. However, this may give the appearance of things not working. Lacking more controlling code,
sample
does not ensure that positions change: it is certainly reasonable to expect that "randomly swap rows" could randomly choose to move row 2 to row 2. Take for example:The first randomly chooses five rows, and then reorders them randomly into an unchanged order. (I suggest that if you want to force that they are all movements, you should ask a new question asking about just forcing a
sample
vector to change.)Anyway, we can regain the original dimensionality with the second function:
In the first plane of the array, rows 1 and 5 are unchanged; in the second plane, rows 1, 2, and 5 are unchanged. Five rows the same, five rows moved around (but otherwise unchanged within each row).