I have a question regarding if-then-else/do statements in R. Based on the internet, my R manuals, and this website, I see that R works differently in this arena than other programming languages because of vectors, but I'm not quite sure I understand and thus am unable to get the following function to work.
I am trying to have R do a calculation based on whether the variable, pc, is 0 or 1. If pc=0, then it should create an average customized for pc=0 or pc=1. The output should be 'avecount'. Here's my code:
avecount<-if(pc==0){sum(sumcount/23)} else if (pc==1){sum(sumcount/12)}else {FALSE}
There are other ways to do this exact same function such as subsetting each by 'pc' and then merging them together in a dataset, but my gut feeling is that this is not the most efficient way to accomplish this. Please help!
Aucun commentaire:
Enregistrer un commentaire