[R] Introduction of NA:s

Jonathan Rougier J.C.Rougier at durham.ac.uk
Wed Oct 2 15:19:54 CEST 2002


Peter Dalgaard BSA wrote:
> 
> "Patrik Waldmann" <patrik.waldmann at djingis.se> writes:
> 
> > Hello,
> >
> > I wonder if someone could help me with the following: I have
> > generated 10 000 values from rnorm and now I want to randomly
> > replace 500 of those with NA. The problem is that values indexed
> > between 6-10,16-20,26-30.... only should be considered for
> > replacement. Any suggestions?
> 
> x <- rnorm(10000)
> x[sample(c(outer(-4:0,seq(10,10000,10),"+")),500)] <- NA
> 
> print(matrix(x,ncol=10,byrow=T),digits=1)

Peter's solution is very lean, but the following might be more
transparent:

> x <- matrix(rnorm(10000), 10)
> x[6:10, ][sample(1:5000, 500)] <- NA
> x <- as.vector(x)

Cheers, Jonathan.

-- 
Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list