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

From: Walter Anderson [mailto:wandrso...@gmail.com]
Sent: Monday, December 09, 2013 5:47 PM
To: PIKAL Petr
Cc: r-help@r-project.org
Subject: Re: [R] Need help figuring out sapply (and similar functions) with 
multiple parameter user defined function

Petr,

Thank you for your assistance; however, your code does not produce the correct 
results in the following two cases;

ID,Q1,Q2,Q3
17,Option 1, Option 3, Option 2
24,Option 2, Option 3, Option 1

In both cases it chooses a preference of Option 1, when the correct answer is 
None

# 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
# Test data that shows all possible responses to the questions
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 10:21 AM, PIKAL Petr wrote:
Hi

you are still rather cryptic. If you said you want to extract prevalent choices 
in each row it would save me (and you) a lot of time.

Import data and make necessary chnages

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

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

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

Then make the object matrix without first column

sr<-survey.results[,-1]
sr<-as.matrix(sr)

Elaborate evaluation function

fff <- function(x) names(which.max(table(x)))
Apply function to matrix

result <-  apply(sr,1, fff)

Bind result with original data

survey.results <- cbind(survey.results, result)

And voila, you shall have reqiured values.

Regards
Petr

From: Walter Anderson [mailto:wandrso...@gmail.com]
Sent: Monday, December 09, 2013 4:37 PM
To: PIKAL Petr
Subject: Re: [R] Need help figuring out sapply (and similar functions) with 
multiple parameter user defined function

Peter.

First, let me thank you for your assistance on this.  Your ideas below for 
eliminating the three functions I had that 'adjusted' the data was received and 
appreciated.  I included that approach in my revised script.  I am only left 
with a for loop and my final preference determining code.  I am including the 
current script and test data (all possible choices included-27).

>From my review of all of the responses, I don't see an alternative to the if 
>then block that tests the state of the three questions or the for loop that 
>executes it.  I made some attempts to use the ifelse statement mentioned in 
>the responses, but couldn't get anything to work.

Again, thanks for your assistance and time on this!

Walter


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