Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a value in a data frame in R?

Tags:

replace

r

I have data frame with 0 values, i want to convert each zero to a random value between 0.1 and 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
like image 581
user3729332 Avatar asked May 05 '15 10:05

user3729332


1 Answers

Could try

indx <- df == 0L
df[indx] <- runif(sum(indx), 0.1, 0.5)
like image 137
David Arenburg Avatar answered Nov 14 '22 14:11

David Arenburg