Insert random NAs in a vector in R as a Loop -


so have data frame consisting of values of 0 , 1. make loop randomly samples 38 of observations , replaces them na. successful in doing 1 iteration, original vector observations replaced following 1 line code:

foo$v2[sample(seq(foo$v2), 38)] <- na 

data frame

however, 20 times , have each iteration compiled separate columns in single object. ultimately, have 20 column data frame each having 191 observations, each 38 randomly substituted na's. @ end of loop, data frame written out text file. thank in right direction.

data set:

https://drive.google.com/file/d/0bxfytpfgcdacdeq2lwfuvwvqmvu/view?usp=sharing

maybe this:

# fake data set.seed(1998) foo = data.frame(v2=sample(0:1, 191, replace=true))  # create 20 samples replace 38 randomly chosen values na n = 20  samples = as.data.frame(replicate(n, {   x = foo$v2   x[sample(seq_along(x), 38)] = na   x   })) 

then can write in whatever format wish. example:

write.table(samples, "samples.txt") write.csv(samples, "samples.csv", row.names=false) 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -