Adam Z. Jabir
2018-Jul-05 20:28 UTC
[R] command to change some vars to missing into my dataset
Hi, I want to simulate missing at random for my dataset. Do you know an easy way to do it? I want to try not to have the missing?s for the same observations. I mean if one observation is been selected randomly to have missing I don?t want to have all the var of the same obs missing. I want to be able to choose rate of missing that should be applied. Thanks, Adam Envoy? ? partir de Outlook<http://aka.ms/weboutlook> [[alternative HTML version deleted]]
Rui Barradas
2018-Jul-05 21:24 UTC
[R] command to change some vars to missing into my dataset
Hello, What type of data do you have? A vector? Or is it a matrix, a data.frame, a list, etc? Suppose it is a vector named x. Then you could do something like rate <- 0.2 is.na(x) <- sample(length(x), rate*length(x)) At an R prompt type ?is.na ?sample Hope this helps, Rui Barradas ?s 21:28 de 05-07-2018, Adam Z. Jabir escreveu:> Hi, > > I want to simulate missing at random for my dataset. Do you know an easy way to do it? > > I want to try not to have the missing?s for the same observations. I mean if one observation is been selected randomly to have missing I don?t want to have all the var of the same obs missing. > > I want to be able to choose rate of missing that should be applied. > > Thanks, > > Adam > > > Envoy? ? partir de Outlook<http://aka.ms/weboutlook> > > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Jim Lemon
2018-Jul-05 22:44 UTC
[R] command to change some vars to missing into my dataset
Hi Adam, Looks like you have a matrix or data frame and want to change one or more observations to NA. I think this will do the trick: # assume the matrix or data frame is named "ajdat" randomNA<-function(x,nNA=1) { dimx<-dim(x) x[sample(1:dimx[1],nNA),sample(1:dimx[2],nNA)]<-NA return(x) } So if you want three NAs inserted, call: randomNA(ajdat,3) Jim On Fri, Jul 6, 2018 at 6:28 AM, Adam Z. Jabir <Adam.Z.Jabir at outlook.fr> wrote:> Hi, > > I want to simulate missing at random for my dataset. Do you know an easy way to do it? > > I want to try not to have the missing?s for the same observations. I mean if one observation is been selected randomly to have missing I don?t want to have all the var of the same obs missing. > > I want to be able to choose rate of missing that should be applied. > > Thanks, > > Adam > > > Envoy? ? partir de Outlook<http://aka.ms/weboutlook> > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Bert Gunter
2018-Jul-05 23:55 UTC
[R] command to change some vars to missing into my dataset
Jim/Rui: Strictly speaking, this is wrong. What you have described is MCAR -- missing completely at random -- not MAR. They are different! Nevertheless, the OP seems to be similarly confused about this, so MCAR may in fact be what what was wanted. Without further context, it is as clear as mud to me. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jul 5, 2018 at 3:44 PM, Jim Lemon <drjimlemon at gmail.com> wrote:> Hi Adam, > Looks like you have a matrix or data frame and want to change one or > more observations to NA. I think this will do the trick: > > # assume the matrix or data frame is named "ajdat" > randomNA<-function(x,nNA=1) { > dimx<-dim(x) > x[sample(1:dimx[1],nNA),sample(1:dimx[2],nNA)]<-NA > return(x) > } > > So if you want three NAs inserted, call: > > randomNA(ajdat,3) > > Jim > > > On Fri, Jul 6, 2018 at 6:28 AM, Adam Z. Jabir <Adam.Z.Jabir at outlook.fr> > wrote: > > Hi, > > > > I want to simulate missing at random for my dataset. Do you know an easy > way to do it? > > > > I want to try not to have the missing?s for the same observations. I > mean if one observation is been selected randomly to have missing I don?t > want to have all the var of the same obs missing. > > > > I want to be able to choose rate of missing that should be applied. > > > > Thanks, > > > > Adam > > > > > > Envoy? ? partir de Outlook<http://aka.ms/weboutlook> > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]