问题
Following up on this question, I want to do something similar, but this time I have one more requirement.
I want to make 2 vectors subsetting from the same data.
I need replace to be set to FALSE because I need all values to be different across a, and all values to be different across b.
Apart from that, values cannot be the same in a and b for the same index position.
Note that sampling vector v is always fixed, as is the sample length l.
Doing the following, I only fulfil one criterium (values across a and values across b are different, but still values in the same index between a and b can be identical)
> set.seed(1)
> v <- 1:15
> l <- 10
> a <- sample(v, l, replace=F)
> b <- sample(v, l, replace=F)
> a
[1] 4 6 8 11 3 9 10 14 5 1
> b
[1] 4 3 9 5 13 12 7 8 14 10
> a==b
[1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Doing the following (answer from the previous question), I only fulfil the other criterium (values in the same index are not identical, but there can be identical values across a or b).
> ab <- split(replicate(10, sample(15,2)), seq(2))
> a <- ab[[1]]
> b <- ab[[2]]
> a
[1] 14 7 10 8 2 6 6 8 13 12
> b
[1] 5 5 4 11 13 12 5 13 6 14
> duplicated(a)
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
> duplicated(b)
[1] FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
Any help trying to collapse both approaches? Thanks!
回答1:
Edited with a while loop to do the resampling until it is correct (with no duplicates).
Got it... What I did was check which values in b are equal to a for the same index, and which are not equal.
For those that are equal, I resample from a vector equal to v but without the already "accepted" values in b (the ones not equal to a for the same index).
Like this:
> set.seed(6)
> v <- 1:15
> l <- 10
> a <- sample(v, l, replace=F)
> b <- sample(v, l, replace=F)
> a
[1] 10 14 4 5 9 15 11 7 13 1
> b
[1] 10 13 2 4 9 3 5 6 14 11
> a==b
[1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
> if (any(a==b)==TRUE) {
+ b0 <- b[which(a==b)]
+ b2 <- b[which(a!=b)]
+ vnew <- v[which(!(v %in% b2))]
+ b0 <- sapply(b0, function(x) sample(vnew[vnew != x], 1))
+ while (any(duplicated(b0))==TRUE){
+ b0 <- sapply(b0, function(x) sample(vnew[vnew != x], 1))
+ }
+ b[which(a==b)] <- b0
+ }
> a
[1] 10 14 4 5 9 15 11 7 13 1
> b
[1] 15 13 2 4 12 3 5 6 14 11
> a==b
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> duplicated(b)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
来源:https://stackoverflow.com/questions/58375977/r-make-2-subset-vectors-so-that-values-are-different-index-wise-and-also-diffe