[R] How do I turn NA's to zeroes when a combination lacks one element?

Mitchell Maltenfort mmalten at gmail.com
Sun Jan 29 19:03:10 CET 2012


?ifelse

Type that in and you'll be in good shape.

On 1/29/12, C_Crown <ccrown at life.bio.sunysb.edu> wrote:
> Hi all, I am  very new to R. I am taking a course and am trying to complete
> my first assignment.
> For the assignment I have to get the program to generate different color
> combinations possible for a sample size of 55 with the probabilities of each
> color being chosen as: .24, .24, .16, .20, .13, .14. Here is what I've come
> up with...
>
> sample.size<- 55
> MM.probability<- c(.24, .24, .16, .20, .13, .14)
> MM.color<- c('Bl','Br','G','O','R','Y')
> mmtable<- matrix(nrow = 1000, ncol = 6)
> for(i in 1:1000){
> combinations<- sample(MM.color, sample.size, replace = T, prob =
> MM.probability)
> mmtable[i,]<-table(combinations)
> colnames(mmtable)<- c("Bl","Br","G","O","R","Y")
> }
>
> I feel like it should work, but every time I run it, it usually only goes so
> far (maybe to row 350, or 450, sometimes it completes with no problem)
> before I start getting "NA" in every column of every row. I also get this
> error message "Error in mmtable[i, ] <- table(combinations) :  number of
> items to replace is not a multiple of replacement length"
> Someone suggested that it is because the program is coming upon a
> combination that is missing one of the colors, so I'd have to instruct it to
> put a zero in place of the missing color so the simulation can continue,
> which completely makes sense. But I've been trying and can't figure out how
> to do it. I tried "mmtable[is.na(mmtable)]<-0", but then I just get zeroes
> everywhere instead of NA's, and "mmtable[na.omit(mmtable)]" gives me the
> same as no instruction at all. Do you know what the right notation would be?
> I also need to use the script to determine the probability of getting the
> combination with proportions: .182, .164, .309, .145, .091, .1090. I've also
> tried a few things for this and am coming up with nothing. Sorry if this is
> a really simple question, I am sure most of you could do this in your sleep,
> but it's all very new to me. Thanks in advance.
>
> -C
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/How-do-I-turn-NA-s-to-zeroes-when-a-combination-lacks-one-element-tp4338725p4338725.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>

-- 
Sent from my mobile device

____________________________
Ersatzistician and Chutzpahthologist
I can answer any question.  "I don't know" is an answer. "I don't know yet"
is a better answer.



More information about the R-help mailing list