Indicator function in R

前端 未结 4 1773
礼貌的吻别
礼貌的吻别 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:14

    If i am able to understand you correctly then you want to make changes into entire data frame,assuming of which i can suggest you to use apply like below, where df is your data frame.

    apply(df,2,function(x)ifelse((x>0),1,0))
    

    You can also use if its for only one vector something like below:

    x <- c(-2,3,1,0)
    y <- ifelse(x>0,1,0)
    print(y)
    [1] 0 1 1 0 #Output
    

    Hope this helps

提交回复
热议问题