C_Crown
2012-Jan-29 17:18 UTC
[R] How do I turn NA's to zeroes when a combination lacks one element?
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.
Berend Hasselman
2012-Jan-29 17:48 UTC
[R] How do I turn NA's to zeroes when a combination lacks one element?
On 29-01-2012, at 18:18, C_Crown 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.This is homework. "Someone's" guess as to what is causing the problem is correct. However: Create mmtable as a matrix with all 0's and give the columns the names of colors. Like this mmtable <- matrix(0, nrow = 1000, ncol = 6, dimnames=list(c(),MM.color)) Then in the loop replace the lines mmtable[i,]<-table(combinations) colnames(mmtable)<- c("Bl","Br","G","O","R","Y") with z <- table(combinations) mmtable[i,names(z)] <- z The second line stores the entries in z in the columns with the same column name and leaves the others alone. Since you initialized mmtable with 0's you won't need to replace NA's with 0. Berend
Mitchell Maltenfort
2012-Jan-29 18:03 UTC
[R] How do I turn NA's to zeroes when a combination lacks one element?
?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.