Petr, Thank you for you assistance. Particularly this last bit. It really helped me understand exactly how your solution worked and why I was confused by rapply processing rows. It was apply that could process rows (and/or columns) of a matrix,
Anyway for anyone who wants the version of my script that incorporates all of the suggested changes, here it is. survey.results <- read.csv("SamplePairedComparisonData.csv") # The following function evaluate which, if any, preference the respondent has shown get.pref <- function(x) { tb <- table(x) if ((length(tb) == 3) & max((tb) == min(tb))) retVal <- "None" # This opton checks for a couple of special cases where the simple which.max function fails to # detect that each of the three items has a single response. else # For all other conditions the following detects which response received the most responses (2+) and records a NONE # in any case that doesn't achieve that level of response. retVal <- names(which.max(table(x))) return(retVal) } # The data is expected to be organized as so: # ID, Q1, Q2, Q3 # and that the questions are in the following format # Q.1) Which do you prefer? # 1) Option 1 # 2) Option 2 # Q.2) Which do you prefer? # 1) Option 1 # 2) Option 3 # Q.3) Which do you prefer? # 1) Option 2 # 2) Option 3 # And the next three lines reprocess the data table to organize the questions so that the values for Q.2) are 1 or 3 and the values for Q.3) are 2 or 3 survey.results[survey.results[,3]==2,3]<-3 # Convert column 3 (Q2) to a value of 3 if the existing value is 2 # The order of the following two commands is important, don't change survey.results[survey.results[,4]==2,4]<-3 # Convert column 4 (Q3) to a value of 3 if the existing value is 1 survey.results[survey.results[,4]==1,4]<-2 # Convert column 4 (Q4) to a value of 2 if the existing value is 1 # The following lines convert the numerical values in the fields to text (factors) # If the questions don't have empty (zero) values than the "None" should be removed from those questions where zero isn't possible # The labels 'Option 1' and 'Option 2' can be replaced with more appropriate text as needed. survey.results$Q1 <- factor(survey.results$Q1, labels=c("None","Option 1","Option 2")) survey.results$Q2 <- factor(survey.results$Q2, labels=c("None","Option 1","Option 3")) survey.results$Q3 <- factor(survey.results$Q3, labels=c("None","Option 2","Option 3")) # Take the survey data table and get rid of the first column (ID) and convert the remaining three columns # to a matrix. Then apply the get.pref function to each of the rows in the matrix survey.results$Preference <- apply(as.matrix(survey.results[,-1]),1,get.pref) On 12/10/2013 05:02 AM, PIKAL Petr wrote: > > In that case you need correct those cases inside the function. > Everything else stays same. > > Something like > > fff <- function(x) { > > tb<-table(x) > > if(max(tb)==min(tb)) res <- "None" else res <- names(which.max(table(x))) > > } > > Regards > > Petr > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.