简体   繁体   English

从特定向量元素采样的ifelse条件

[英]ifelse condition with sampling from specific vector element

I have four vectors: 我有四个向量:

a <- sample(1:2,10,replace=T)
b <- sample(1:2,10,replace=T)
c <- sample(4:5,10,replace=T)
d <- sample(4:5,10,replace=T)

I would like to compare each element of c with its corresponding element in d and do the following: 我想将c每个元素与其在d对应元素进行比较,然后执行以下操作:

ifelse(c>d,a,ifelse(c==d,SAMPLE(a,b),replace=T),b)

a: [1] 1 1 2 2 2 1 1 2 1 2 a: [1] 1 1 2 2 2 1 1 2 1 2

b: [1] 2 1 2 1 2 2 1 2 2 1 b: [1] 2 1 2 1 2 2 1 2 2 1

c: [1] 4 4 4 4 4 4 5 5 4 4 c: [1] 4 4 4 4 4 4 5 5 4 4

d: [1] 4 4 4 5 5 5 4 4 5 4 In words: d: [1] 4 4 4 5 5 5 4 4 5 4用词表示:

  1. if the first element of c is larger than the first element of d: return a 如果c的第一个元素大于d的第一个元素:返回a
  2. if the first element of c is equal to the first element of d choose randomly between a and b 如果c的第一个元素等于d的第一个元素,则在a和b之间随机选择
  3. if the first element of c is smaller than d return b 如果c的第一个元素小于d,则返回b
  4. repeat for each element from 1 to 10. 对1到10的每个元素重复上述步骤。

What I am having trouble with is point 2. How can I tell R to sample only from a given element and not the whole vector: 我遇到的问题是第2点。如何告诉R仅从给定元素采样而不是整个向量采样:

here's the output I would like to see: 这是我想看到的输出:

sample[1,2], sample[1,1], sample[2,2], b, b, b, a, a,b, sample[2,1]

If a == b then there isn't really any point making a sample right? 如果a == b那么实际上没有任何必要做一个sample吗? Because both a and b are the same? 因为ab相同? So you would be randomly choosing between the same number?! 因此,您将在相同的数字之间随机选择?

I'd just use pmax to return the greatest element at each postion: 我只是使用pmax在每个位置返回最大元素:

pmax( a , b )
[1] 2 1 2 2 2 2 1 2 2 2

From the help page for pmax : pmax的帮助页面:

pmax and pmin take one or more vectors (or matrices) as arguments and return a single vector giving the 'parallel' maxima (or minima) of the vectors. pmaxpmin将一个或多个向量(或矩阵)作为自变量,并返回单个向量,以给出向量的“平行”最大值(或最小值)。 The first element of the result is the maximum (minimum) of the first elements of all the arguments, the second element of the result is the maximum (minimum) of the second elements of all the arguments and so on. 结果的第一个元素是所有参数的第一个元素的最大值(最小值),结果的第二个元素是所有参数的第二个元素的最大值(最小值),依此类推。

Edit due to new information from OP 根据OP中的新信息进行编辑

Here's one way to do what you want with three vectors (I just wrote this example up and saw you posted something with 4). 这是用三个向量做您想要的事情的一种方法(我只是写了这个例子,看到您用4张贴了一些东西)。 This approach readily translates to your updated OP, I'll leave you to work out how. 这种方法很容易转换为您更新的OP,我将让您研究如何操作。 When posting data generated through RNG its a good idea to specify the seed so someone can reproduce your vectors. 发布通过RNG生成的数据时,最好指定种子,以便有人可以复制您的向量。

set.seed(1234)
a <- sample( 2 , 10 , repl = T ); b <- sample( 2 , 10 , repl=T ); c <- sample(5 , 10 , repl = T )
#a
#[1] 1 2 2 2 2 2 1 1 2 2
#b
#[1] 2 2 1 2 1 2 1 1 1 1
#c
#[1] 2 2 1 1 2 5 3 5 5 1

#  Empty output vector
out <- rep( NA , length( a ) )

#  Fill elements where a does not equal b with the pmax value of a or b
out[ a != b ] <- pmax( a[ a != b ] , b[ a != b ] )

#  Indicies where a == b 
ind <- a == b

#  Fill elements where a == b with a random choice of a or c for that element
set.seed(1)
out[ ind ] <- cbind( a[ind] , c[ind] )[ cbind( seq_len( sum( ind ) ) , sample( 2 , sum( ind ) , repl = TRUE ) ) ]
#[1] 2 2 2 2 2 5 3 1 2 2

I am just tinkering, but how about this: 我只是在修补,但这又如何:

pswitch <- function(x,...) diag(sapply(x,switch,...))
pswitch(sign(c-d)+2,b,ifelse(runif(10)>.5,a,b),a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM