Replace all values lower than threshold in R

前端 未结 4 1830
予麋鹿
予麋鹿 2020-12-11 03:09

I have a vector

x <- c(-1, 0, 1, 2, 3)

I want all values less than 1 to be replaced by 1.

How?

Is there a no-loop so

4条回答
  •  -上瘾入骨i
    2020-12-11 03:37

    Another option would be replace:

    x <- c(-1, 0, 1, 2, 3)
    
    replace(x, x < 1,1)
    # [1] 1 1 1 2 3
    

提交回复
热议问题