Convert negative values to zero in dataframe in R

后端 未结 5 1970

I have a dataframe in R that I would like to convert all columns (outside the ids) from negative to zero

id1  id2  var1  var2  var3
-1   -1   0     -33     5         


        
5条回答
  •  太阳男子
    2021-01-05 01:50

    You can try using replace:

    > mydf[-c(1, 2)] <- replace(mydf[-c(1, 2)], mydf[-c(1, 2)] < 0, 0)
    > mydf
      id1 id2 var1 var2 var3
    1  -1  -1    0    0    5
    2  -1  -2    9    0    0
    

提交回复
热议问题