[R] Need help figuring out sapply (and similar functions) with multiple parameter user defined function

Walter Anderson wandrson01 at gmail.com
Fri Dec 6 17:11:21 CET 2013


Thank you for your response!

I am attempting to determine a preference from the answers to three 
binomial questions;

q.1) 1 or 2    q.2) 1 or 3    q.3) 2 or 3

However, the questions are coded with either a 1 or 2 (though no answer 
is also possible) and the first three functions (q#.ans) convert those 
values to the 1,2,or 3 shown above

and generate one of the following result for each row of the table; 0 - 
no preference, or 1,2,3 which indicates the preference indicated by the 
question

The if's implement the following state conditions:

   # ID A  B  C  Preference
   # 1  0  0  0  None
   # 2  0  0  1  None
   # 3  0  0  2  None
   # 4  0  1  0  None
   # 5  0  1  1  Option 1
   # 6  0  1  2  None
   # 7  0  2  0  None
   # 8  0  2  1  None
   # 9  0  2  2  Option 2
   # 10 1  0  0  None
   # 11 1  0  1  Option 1
   # 12 1  0  2  None
   # 13 1  1  0  Option 1
   # 14 1  1  1  Option 1
   # 15 1  1  2  Option 1
   # 16 1  2  0  None
   # 17 1  2  1  Option 1
   # 18 1  2  2  Option 2
   # 19 2  0  0  None
   # 20 2  0  1  None
   # 21 2  0  2  Option 2
   # 22 2  1  0  None
   # 23 2  1  1  Option 1
   # 24 2  1  2  Option 2
   # 25 2  2  0  Option 2
   # 26 2  2  1  Option 2
   # 27 2  2  2  Option 2

The if statement only implements those values from the state machine 
that show a preference (ID's 5,9,11,13-15,17-18,21,23-27)

On 12/06/2013 09:59 AM, PIKAL Petr wrote:
> Hi
>
> The warning is due to fact that "if" takes only single scalar value not an entire vector.
>
> Maybe you shall explain more clearly what result do you expect.
>
> I bet that there is vectorised solution to your problem but I am lost in your ifs and cannot follow what shall be the output.
>
> Please use
>
> dput(head(df))
>
> when showing input data and clearly describe intended result.
>
> Regards
> Petr
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Walter Anderson
>> Sent: Friday, December 06, 2013 4:44 PM
>> To: r-help at r-project.org
>> Subject: [R] Need help figuring out sapply (and similar functions) with
>> multiple parameter user defined function
>>
>> I am having trouble understanding how to use sapply (or similar
>> functions) with a user defined function with multiple parameters.
>>
>> I have the following functions defined
>>
>>      q1.ans <- function(x)
>>      {
>>         retVal = 0
>>         if (x == 1) {
>>           retVal = 1
>>         } else if (x ==2) {
>>           retVal = 2
>>         }
>>         return (retVal)
>>      }
>>      q2.ans <- function(x)
>>      {
>>         retVal = 0
>>         if (x == 1) {
>>           retVal = 1
>>         } else if (x ==2) {
>>           retVal = 3
>>         }
>>         return (retVal)
>>      }
>>      q3.ans <- function(x)
>>      {
>>         retVal = 0
>>         if (x == 1) {
>>           retVal = 2
>>         } else if (x ==2) {
>>           retVal = 3
>>         }
>>         return (retVal)
>>      }
>>
>>      evaluate.questions <- function(q.1,q.2,q.3)
>>      {
>>         a <- q1.ans(q.1)
>>         b <- q2.ans(q.2)
>>         c <- q3.ans(q.3)
>>         retVal = 0   # Set default value to be no preference
>>         # The following code only implements those values from the state
>>      machine that show a preference (ID's 5,9,11,13-15,17-18,21,23-27)
>>         if (a == 0) {
>>           if (b == 1) {
>>             if (c == 1) {
>>               retVal = 1  # State machine ID 5
>>             }
>>           } else if (b == 2) {
>>             if (c == 2) {
>>               retVal = 2  # State machine ID 9
>>             }
>>           }
>>         } else if (a == 1) {
>>           if (b == 0) {
>>             if (c == 1) {
>>               retVal = 1  # State machine ID 11
>>             }
>>           } else if (b == 1) {
>>             retVal = 1    # State machine ID's 13-15, value of C doesn't
>>      matter
>>           } else if (b == 2) {
>>             if (c == 1) {
>>               retVal = 1  # State machine ID 17
>>             } else if (c == 2) {
>>               retVal = 2  # State machine ID 18
>>             }
>>           }
>>         } else if (a == 2) {
>>           if (b == 0) {
>>             if (c == 2) {
>>               retVal = 2  # State machine ID 21
>>             }
>>           } else if (b == 1) {
>>             if (c == 1) {
>>               retVal = 1  # State machine ID 23
>>             } else if (c == 2) {
>>               retVal = 2  # State machine ID 24
>>             }
>>           } else if (b == 2) {
>>             retVal = 2    # State machine ID's 25-27, value of C doesn't
>>      matter
>>           }
>>         }
>>         return (retVal)
>>      }
>>
>> And a data set that looks like this:
>>
>>      ID,Q1,Q2,Q3
>>      1,2,2,2
>>      2,2,1,1
>>      3,1,1,1
>>      4,1,2,2
>>      5,2,2,1
>>      6,1,2,1
>>      ...
>>
>>
>> I have been researching and it appears that I should be using the
>> sapply function to apply the evaluate.question function above to each
>> row in the data frame like this
>>
>> preferences <- sapply(df, evaluate.questions, function(x,y,z)
>> evaluate.questions(df['Q1'],df['Q2'],df['Q3']))
>>
>> Unfortunately this doesn't work and the problem appears that the sapply
>> function is not feeding the parameters to the evaluate.questions
>> function as I expect.  Can someone provide some guidance on what I am
>> doing wrong?
>>
>> This is the error message I am getting:
>>
>> Error in x --1 :
>>     Comparison (1) is possible only for atomic and list types In
>> addition: warning messages:
>> In if (x == 1) { :
>>     the condition has length > 1 and only the first element will be used
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.



More information about the R-help mailing list