向量的每个元素上的ifelse [英] ifelse over each element of a vector
问题描述
看这篇文章,我认为ifelse
是f(c(x1, x2, x3)) = c(f(x1), f(x2), f(x3))
的向量化.
Looking at this post, I thought ifelse
is vectorized in the sense that f(c(x1, x2, x3)) = c(f(x1), f(x2), f(x3))
.
所以,我认为z1
的代码(下面提供)是否会对向量y
的每个元素执行以下操作:
So, I thought if the code for z1
(provided below) will perform the following for each element of the vector y
:
- 测试是否统一.
- 如果是,请从{1、3、5、7、9}中抽取一个随机数.
- 如果否,则从{0,2,4,6,8}中抽取一个随机数.
- Test whether it is unity or not.
- If YES, draw a random number from {1, 3, 5, 7, 9}.
- If NO, draw a random number from {0, 2, 4, 6, 8}.
但是,不幸的是,它没有做到这一点.它为每种情况生成一次,并始终返回该非常随机的数字.
But, unfortunately it doesn't do that. It generates once for each case, and returns that very random number always.
我到底在哪里做错了?还是
ifelse
的预期行为?Where exactly am I doing wrong? Or, is it actually the expected behaviour of
ifelse
?请注意,如果将其用作
sapply
中的包装器函数,则会得到预期的输出z2
(从某种意义上说,它不能确定为z1
,在这种情况下,观察到每种情况都足够了),如下所示.Just to note, if I use this as a wrapper function inside
sapply
, I get the expected outputz2
(in the sense that it is not deterministic asz1
where observing one occurrence of each case is enough), as you can see below.y <- rbinom(n = 20, size = 1, prob = 0.5) z1 <- ifelse(test = (y == 1), yes = sample(x = c(1, 3, 5, 7, 9), size = 1), no = sample(x = c(0, 2, 4, 6, 8), size = 1)) z2 <- sapply(X = y, FUN = function(w) { ifelse(test = (w == 1), yes = sample(x = c(1, 3, 5, 7, 9), size = 1), no = sample(x = c(0, 2, 4, 6, 8), size = 1)) }) data.frame(y, z1, z2) #> y z1 z2 #> 1 0 2 2 #> 2 1 1 3 #> 3 1 1 9 #> 4 1 1 7 #> 5 0 2 0 #> 6 0 2 2 #> 7 1 1 7 #> 8 1 1 7 #> 9 0 2 0 #> 10 1 1 5 #> 11 0 2 0 #> 12 0 2 0 #> 13 0 2 6 #> 14 0 2 0 #> 15 0 2 2 #> 16 1 1 7 #> 17 1 1 7 #> 18 0 2 2 #> 19 0 2 2 #> 20 0 2 0 unique(x = z1[y == 1]) #> [1] 1 unique(x = z1[y == 0]) #> [1] 2
由 reprex软件包(v0.2.1)创建于2019-03-13
Created on 2019-03-13 by the reprex package (v0.2.1)
任何帮助将不胜感激.
推荐答案
ifelse
不是一个向量的函数,而是 3个相同长度的向量的函数.第一个向量test
是布尔值,第二个向量yes
和第三个向量no
给出结果中的元素,并根据test
值逐项选择.ifelse
isn't a function of one vector, it is a function of 3 vectors of the same length. The first vector, calledtest
, is a boolean, the second vectoryes
and third vectorno
give the elements in the result, chosen item-by-item based on thetest
value.size = 1
的样本的大小与test
的大小不同(除非test
的长度为1),所以它将由ifelse
回收(请参见下面的注释).相反,请从头开始绘制与test
相同大小的样本:A sample of
size = 1
is a different size thantest
(unless the length oftest
is 1), so it will be recycled byifelse
(see note below). Instead, draw samples of the same size astest
from the start:ifelse( test = (y == 1), yes = sample(x = c(1, 3, 5, 7, 9), size = length(y), replace = TRUE), no = sample(x = c(0, 2, 4, 6, 8), size = lenght(y), replace = TRUE) )
向量实际上不必具有相同的长度.帮助页面
?ifelse
解释:如果yes
或no
太短,则其元素将被回收."这是您在上观察到的行为"大小写,并始终返回该非常随机的数字..The vectors don't actually have to be of the same length. The help page
?ifelse
explains: "Ifyes
orno
are too short, their elements are recycled." This is the behavior you observed with "It generates once for each case, and returns that very random number always.".这篇关于向量的每个元素上的ifelse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!