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

PIKAL Petr petr.pikal at precheza.cz
Fri Dec 6 16:59:24 CET 2013


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