How to replace value in data frame in R?

I have a data frame with 0 values, I want to convert every zero to a random value from 0.1 to 0.5

X2 X3 X4 X5 390 0 0.000 0.00 0.00 7433 0 27.839 0.00 0.00 5579 0 0.000 151.95 0.00 

to be

  X2 X3 X4 X5 390 0.1 0.200 0.43 0.29 7433 0.3 27.839 0.24 0.30 5579 0.4 0.200 151.95 0.50 
+5
source share
1 answer

You can also try

 indx <- df == 0L df[indx] <- runif(sum(indx), 0.1, 0.5) 
+9
source

All Articles