I am fairly familiar with R but have reached a point where my data demands require me to learn iterative loops with multiple conditions. I have seen examples using various forms of *apply(), as well as colSums() and rowSums() used to perform the type(s) of data transformations that I need, but I want to increase the efficiency of these tasks, perhaps nesting or iterating a loop. Also, existing recommendations do not take into account data loss from ignoring/dropping "NA" items and I need to be able to retain this information.
my general data format is as follows:
group <- c("A", "B", "C", "A", "C" [...])
individual <- c("1", "2", "3", "4", "5" [...])
choice1 <- c("1", "0", "1", "1", "NA")
choice2 <- c("1", "NA", "1", "0", "NA")
[...]
choice10 <- c("1", "0", "1", "1", "NA")
I need to calculate a count of each of the three choices; 1==yes; 0==no; NA==opt-out of choice across choices and across groups, and then convert these in to percentages. Where I have encountered the most difficulty with previous methods like *apply() or Summation across row/column is that my "NA" values (opt outs) are ignored, or prevent me from being able to adequately take percentages of choice values across groups. Any specific advice or demonstration of how to either ignore OR retain the "opt outs"/NAs within the loop structure would be greatly appreciated.
The output would look a bit like the following: yes.count_bychoice
no.count_bychoice
optout.count_bychoice
percentyes_bychoice_bygroup
percentno_bychoice_bygroup
percentout_bychoice_bygroup