LivinLife
LivinLife

Reputation: 109

ifelse Functions in R

I'm trying to make a function that will print off the prize if function matches the 5,1 or 5,0.

Is this even possible with the below? I've tried various ways and manage to receive the same error:

powerball_numbers(5,1)

  Error in powerball_numbers(5, 1) : unused argument (1)

  # total Grand Prize
  grand_prize <- 10000000
  #M5 white balls and one red
  w_5_r_1 <- c(5,1)

 #Second Prize of $1,000,000
 second_prize <- 50000
 #Match 5 white balls and 0 Red
 w_5_r_0 <- c(5,0)

  powerball_numbers <- function(x) {ifelse(x = w_5_r_1, grand_prize) 
                              ifelse(x = w_5_r_0, second_prize)
                              }
  powerball_numbers(5,1)

   Error in powerball_numbers(5, 1) : unused argument (1)

Upvotes: 0

Views: 98

Answers (1)

denis
denis

Reputation: 5673

Various mistakes here. In ifelse, the condition should use == for number or character or factors.

Then your function takes only one argument, and you give two.The argument should be c(5,1), and not 5,1, that are two numbers

And third you can't compare the equality of a vector or list with ==, but should use identical() function

and the idea of ifelse is to provide both answer for the tRUE and the False condition

 powerball_numbers <- function(x) {if(identical(x,w_5_r_1)){grand_prize} else if(identical(x,w_5_r_0)){second_prize}}
 powerball_numbers(c(5,1))
[1] 1e+07

Upvotes: 1

Related Questions