Consider the following data set:
df <- data.frame(id=1:10,
v1=c(2.35456185,1.44501001,2.98712312,0.12345123,0.96781234,
1.23934551,5.00212233,4.34120000,1.23443213,0.00112233),
v2=c(0.22222222,0.00123456,2.19024869,0.00012000,0.00029848,
0.12348888,0.46236577,0.85757000,0.05479729,0.00001202))
My intention is to round the values in v1
and v2
to the nearest one decimal place (10% of observation), two decimals (40% of observations), and three decimal places (50% of observations) randomly. I can use the round()
function to round numbers to certain decimal places uniformly. In my case, however, it's not uniform. Thank you in advance!
Example of output needed (of course mine is not random):
id v1 v2
1 2.3 0.2
2 1.45 0
3 2.99 2.19
4 0.12 0
5 0.97 0
6 1.239 0.123
7 5.002 0.462
8 4.341 0.858
9 1.234 0.055
10 0.001 0