Quick Tip: Replace Values in Dataframe on Condition with Random Numbers

May 1, 2012
By

(This article was first published on theBioBucket*, and kindly contributed to R-bloggers)

This one took me some time – though, in fact it is plain simple:

> options(scipen=999)
> (my_df <- data.frame(matrix(sample(c(0,1), 100, replace = T), 10, 10)))
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 0 0 1 0 1 1 1 1 0 1
2 0 0 1 1 1 0 0 0 0 0
3 0 1 1 0 0 1 1 0 0 1
4 1 1 0 0 0 0 0 0 0 0
5 0 0 1 1 1 0 0 1 1 1
6 0 0 1 1 0 0 1 1 0 1
7 0 0 1 1 1 0 1 1 1 1
8 1 1 1 0 0 0 0 1 1 0
9 0 0 0 0 1 0 1 0 1 0
10 1 1 1 1 1 0 1 1 0 1
> my_df[my_df == 0] <- runif(sum(my_df==0), 0, 0.001)
> my_df
X1 X2 X3 X4 X5 X6 X7 X8
1 0.000268 0.000926 1.000000 2.00e-05 1.000000 1.00e+00 1.00e+00 1.00e+00
2 0.000531 0.000882 1.000000 1.00e+00 1.000000 4.66e-04 3.96e-04 6.70e-04
3 0.000785 1.000000 1.000000 5.03e-04 0.000164 1.00e+00 1.00e+00 2.98e-04
4 1.000000 1.000000 0.000336 8.71e-04 0.000770 7.44e-05 6.49e-05 1.01e-04
5 0.000168 0.000674 1.000000 1.00e+00 1.000000 6.49e-04 2.26e-04 1.00e+00
6 0.000404 0.000950 1.000000 1.00e+00 0.000735 7.59e-04 1.00e+00 1.00e+00
7 0.000472 0.000516 1.000000 1.00e+00 1.000000 1.37e-04 1.00e+00 1.00e+00
8 1.000000 1.000000 1.000000 6.30e-06 0.000972 3.97e-04 5.46e-05 1.00e+00
9 0.000868 0.000577 0.000347 7.21e-05 1.000000 2.25e-04 1.00e+00 7.19e-05
10 1.000000 1.000000 1.000000 1.00e+00 1.000000 5.80e-05 1.00e+00 1.00e+00
X9 X10
1 0.000880 1.00e+00
2 0.000754 7.99e-04
3 0.000817 1.00e+00
4 0.000982 7.85e-04
5 1.000000 1.00e+00
6 0.000104 1.00e+00
7 1.000000 1.00e+00
8 1.000000 9.43e-06
9 1.000000 7.79e-04
10 0.000099 1.00e+00

To leave a comment for the author, please follow the link and comment on their blog: theBioBucket*.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Recent popular posts

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training



http://www.eoda.de







ODSC

ODSC

CRC R books series





Six Sigma Online Training





Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)