Indicator function in R

前端 未结 4 1782
礼貌的吻别
礼貌的吻别 2020-12-19 07:53

I\'m looking for an indicator function in R, i.e. a function that returns a 1, if the value of an element in a vector is greater than 0 and returns zero, if the value of an

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 08:06

    The I function in R, called the Inhibit Interpretation/Conversion of Objects function, can be used for this purpose. For instance, the line below returns the values for the function I(x < 4) where X = {0, 1, 2, 3, 4, 5}:

    > I(0:5 < 4)
    [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE
    

    In R TRUE and FALSE can be treated as 1 and 0s, but if you insist on your output being precisely those numbers, just wrap your I function into as.numeric.

提交回复
热议问题