if()
期望括号中的任何内容计算为长度为 1 (TRUE
或FALSE
) 的逻辑向量。如果向量长于 1,则如警告所示,仅使用向量的第一个元素。
如果ifelse()
将其用作:
ifelse(test, TRUE_CASE, FALSE_CASE)
对于您的示例,我们有:
set.seed(1)
ifelse(runif(50, 0, 1) < 0.69, rnorm(50, 25, 4), rnorm(50, 28, 4.3))
或者
set.seed(1)
test <- runif(50, 0, 1)
Tcase <- rnorm(50, 25, 4)
Fcase <- rnorm(50, 28, 4.3)
ifelse(test, Tcase, Fcase)
两者都可以,因为在进行比较之前和之前ifelse()
生成两个向量。TRUE_CASE
FALSE_CASE
示例输出是:
R> ifelse(test < 0.69, Tcase, Fcase)
[1] 24.77549 24.37682 19.11699 28.31967 26.67177 25.55472 27.41873 26.55069
[9] 24.78478 19.49176 23.34002 23.42284 24.76275 29.40010 29.14852 24.34191
[17] 33.19383 32.98973 27.22665 34.82338 30.40149 26.45833 28.07413 24.55062
[25] 28.52443 26.59242 22.55189 26.36448 28.67952 30.73209 32.92160 23.53111
[33] 20.82346 27.27888 35.23336 34.60647 26.01493 27.75896 25.20201 22.02691
[41] 26.31093 17.78017 26.79981 25.61301 33.69045 25.82438 22.16021 27.44291
[49] 27.22791 27.56918
另一种方法是只生成所需的值
want <- test < 0.69
res <- test ## copy vector of correct length
res[want] <- rnorm(S <- sum(want), 25, 4)
res[!want] <- rnorm(length(res) - S, 28, 4.3)
R> res
[1] 27.85067 24.70574 24.84946 22.04696 22.27336 36.03795 29.82793 23.70292
[9] 25.24064 22.64442 27.12598 18.92642 26.22623 18.85420 26.97382 23.79610
[17] 32.55148 31.81162 22.88688 25.33725 37.48624 22.39162 24.77241 17.34256
[25] 29.70633 18.34011 23.14588 20.53632 26.90338 21.99672 33.34867 25.06958
[33] 19.85480 18.43758 21.87467 26.80075 27.37908 24.92576 28.89241 23.72773
[41] 37.92431 21.28255 28.45495 19.05016 20.69923 29.96509 29.00012 22.51493
[49] 27.66824 26.56380
这显然与之前的数字不同,但这仅仅是因为我们生成的随机数更少。我怀疑这会比ifelse()
大多数问题更有效。