Convert values below a threshold into 1

前端 未结 3 586
离开以前
离开以前 2021-01-13 19:47

Let\'s say if I have the following data

x <- rnorm(100)

I want to create another column where where if x is equal to or gre

3条回答
  •  无人共我
    2021-01-13 20:18

    We can create the logical condition and wrap with +

    xNew <- +(x >=0.2)
    

    If we need a data.frame,

    dat <- data.frame(x, xNew)
    

    Or use ifelse

    xNew <- ifelse(x >= 0.2, 1, 0) 
    

提交回复
热议问题