0

for testing purposes I need to add missing values to a data frame which has no missing values, how can I add 10% random NAs to my data frame:

dat <- data.frame(v1=rnorm(20),v2=rnorm(20),v3=rnorm(20))

my idea was something like:

a <- sample(1:nrow(dat),3,replace=F)
b <- sample(1:ncol(dat),2,replace=F)

dat[a,b] <- NA

but this is not random enough. thanks.

spore234
  • 3,550
  • 6
  • 50
  • 76
  • 2
    See [here](http://stackoverflow.com/questions/27454265/r-randomly-insert-nas-into-dataframe-proportionaly), [here](http://stackoverflow.com/questions/20873078/how-do-i-add-random-nas-into-a-data-frame) and [here](https://trinkerrstuff.wordpress.com/2012/05/02/function-to-generate-a-random-data-set/) – zx8754 Apr 15 '15 at 13:00

1 Answers1

2

It seems like you're asking for a way to create true random numbers, rather than how to implement it. If this is the case, you could use the random package available through CRAN which can sample random integers from random.org

install.packages("random")
require("random")

The details of the package: http://cran.r-project.org/web/packages/random/index.html

I suggest you look especially at the vignette, 'random: an R package for true random numbers' for how to obtain random integers.

Phil
  • 4,344
  • 2
  • 23
  • 33