向量的每个元素上的ifelse [英] ifelse over each element of a vector

查看:94
本文介绍了向量的每个元素上的ifelse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章,我认为ifelsef(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 output z2 (in the sense that it is not deterministic as z1 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, called test, is a boolean, the second vector yes and third vector no give the elements in the result, chosen item-by-item based on the test value.

      size = 1的样本的大小与test的大小不同(除非test的长度为1),所以它将由ifelse回收(请参见下面的注释).相反,请从头开始绘制与test相同大小的样本:

      A sample of size = 1 is a different size than test (unless the length of test is 1), so it will be recycled by ifelse (see note below). Instead, draw samples of the same size as test 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解释:如果yesno太短,则其元素将被回收."这是您在上观察到的行为"大小写,并始终返回该非常随机的数字..

      The vectors don't actually have to be of the same length. The help page ?ifelse explains: "If yes or no 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆