Skip to content

Introduction of NA:s

6 messages · Patrik Waldmann, Peter Dalgaard, John Fox +3 more

#
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?

Patrik Waldmann


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Patrik Waldmann" <patrik.waldmann at djingis.se> writes:
x <- rnorm(10000)
x[sample(c(outer(-4:0,seq(10,10000,10),"+")),500)] <- NA

print(matrix(x,ncol=10,byrow=T),digits=1)
#
Dear Patrik,
At 12:01 PM 10/2/2002 +0200, Patrik Waldmann wrote:
There's probably a cleverer way of doing it, but the following should work:

         nums <- 1:10000
         data <- rnorm(10000)
         data[sample(nums[is.element(nums - 10*floor(nums/10), c(0,6:9))], 
500)] <- NA

I hope that this helps,
  John

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
You could use the following:

x <- rnorm(10000)
ind <- unlist(matrix(1:10000,byrow=T, ncol=10)[,6:10])
selection <- sample(ind, 500)
x[selection] <- NA


I am pretty sure there is a quicker way to create selection vector...

Eric
At 12:01 2/10/2002 +0200, you wrote:
__________________________________________________

Eric Lecoutre           Informaticien/Statisticien
Institut de Statistique                        UCL

                               (+32) (0)10 47 30 50
                            lecoutre at stat.ucl.ac.be
     http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

__________________________________________________
Le vrai danger, ce n'est pas quand les ordinateurs
penseront comme des hommes, c'est quand les hommes
penseront comme des ordinateurs.     Sydney Harris


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Patrik Waldmann wrote:
is.na(MyData[sample(outer(10 * 0:999, 6:10, "+"), 500)]) <- TRUE

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Peter Dalgaard BSA wrote:
Peter's solution is very lean, but the following might be more
transparent:
Cheers, Jonathan.