Karen,

Thank you for your reply.  That approach makes the code for determining 
preference much clearer.  Unfortunately, it appears I am still needing 
to use a for loop to process each row.

For any who find these posts and may have similar questions, I am 
including my current code and sample data to provide a working solution:

survey.results <- read.csv("SamplePairedComparisonData.csv")

# The following function evaluates which, if any, preference the 
respondent has shown
get.pref <- function(x)
{
   # names(which.max(table(x)))
   n.1 <- sum(x==1) # Calculate the number of times the respondent chose 
option 1
   n.2 <- sum(x==2) # Calculate the number of times the respondent chose 
option 2
   n.3 <- sum(x==3) # Calculate the number of times the respondent chose 
option 3
   retVal <- "None"
   if (n.1 >= 2) {
     retVal <- "Option 1"
   } else if (n.2 >= 2) {
     retVal <- "Option 2"
   } else if (n.3 >= 2) {
     retVal <- "Option 3"
   }
   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 (Q3) to a 
value of 2 if the existing value is 1

for (indx in 1:length(survey.results$ID))
{
   survey.results$Preference[indx] <- get.pref(survey.results[indx, 2:4])
}

# 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
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"))

# survey.results$Preference <- 
apply(as.matrix(survey.results[,-1]),1,get.pref)


and here is the sample data.
#The following sample data represents every possible response to the 
three questions of the type described above
ID,Q1,Q2,Q3
1,0,0,0
2,0,0,1
3,0,0,2
4,0,1,0
5,0,1,1
6,0,1,2
7,0,2,0
8,0,2,1
9,0,2,2
10,1,0,0
11,1,0,1
12,1,0,2
13,1,1,0
14,1,1,1
15,1,1,2
16,1,2,0
17,1,2,1
18,1,2,2
19,2,0,0
20,2,0,1
21,2,0,2
22,2,1,0
23,2,1,1
24,2,1,2
25,2,2,0
26,2,2,1
27,2,2,2

On 12/09/2013 09:42 AM, Karen Keating wrote:
> Hi Walter,
> From your list of preferences, it seems that Option 2 is preferred if 
> there are at least two 2's for the ID, and Option 1 is preferred if 
> there are at least two 1's for the ID.  If neither of these conditions 
> are met, then there is no preference.  If this interpretation is 
> correct, I would suggest replacing your list of 'if' statements with 
> the following:
>
> get.pref<-function(x) {
>  n.1<-sum(x==1)
>  n.2<-sum(x==2)
>  pref='None'
>  if(n.1>=2) {pref='Option 1'} else
>  if(n.2>=2) {pref='Option 2'}
>  pref
> }
> R-help@r-project.org <mailto: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.
>


        [[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.

Reply via email to