Analyze Data using R - Introduction to Programming in R

Hiya

So I’m stuck on one of the activities and I can’t work out what I’ve done wrong.

Use a series of if , else , and else if statements to code the following scenario:

You just heard about a top-secret event where you have to confirm a series of checks in order to enter. In the workspace, you’re given the access_event and access_music_show variables that start at FALSE , and the age , possible_codeword , and real_codeword variables as well.

First, there should be a check on whether age is 21+ or 18+.

  • If age is 21 or above, set access_event to TRUE and print "I got in for the night!" . If 21+, you can also check whether you have the right codeword to access the secret music show at 11. If your possible_codeword matches the real_codeword using the == comparison operator, set access_music_show to TRUE and print "I need to get a good spot!" . Hint: this codeword check should be a nested conditional.
  • But if age is 18+ but not 21+, you have to check whether the current_hour is earlier than 11 . If it is, you can set access_event to TRUE and you should print "I can stay until 11! “ Hint: you can either nest the first check into the second, or use an & (AND) operator to combine them.
  • If age is not 18+ or 21+, both access_event and access_music_show should stay FALSE and you should print "I guess I'll go somewhere else."

title: “Conditionals challenge”
output: html_notebook

# defining the variables
access_event <- FALSE
access_music_show <- FALSE
age <- 22
current_hour <- 9
possible_codeword <- "redrobbin"
real_codeword <- "bluejay"
# conditional code goes here:

if (age >= 21) {
  access_event <- TRUE
  print("I got in for the night!")
   # execute code and don't check any more conditions
} 
else if (possible_codeword == real_codeword) {
  access_music_show <- TRUE
  print("I need to get a good spot!"
   # execute code only if both are true and don't check any more conditions
} 
else if (age >= 18 != 21 | current_hour < 11) {
  access_event <- TRUE
  print("I can stay until 11!")
   # execute code if either condition is true and don't go to else
} 
else (age < 18) {
  access_event & access_music_show <- FALSE
  print("I guess I'll go somewhere else.")
   # the default code if none of the conditions above are true
}

Just here, you try to chain the operators on age, however this is not something you can do in R. If you want the whole age part to be checked in 1 section, you can use & and brackets like so:

else if ((age >= 18 & age != 21) | current_hour < 11)

and this should do the job.

Hi Adam,

Thanks for that. So I gave it a good, but its still throwing the error: Did you include an if block, and else if block, and an else block?

There’s another issue I can find which is here:

You either want to use else if with that condition, or remove the condition and just use else. You cannot use else (condition) as else implies all scenarios other than those covered.

EDIT: I would also say to amend my previous message, after reading over the description closer, the second else if is more complicated than it needs to be. You have already checked to see if age >= 21 in the first if condition, so if this else is being reached it is implied that age is less than 21. Therefore you don’t need to specify age != 21 and can instead just do

else if (age >= 18 | current_hour < 11) {#execute code}

I found the solution, finally:


title: “Conditionals challenge”
output: html_notebook

# defining the variables
access_event <- FALSE
access_music_show <- FALSE
age <- 22
current_hour <- 9
possible_codeword <- "redrobbin"
real_codeword <- "bluejay"
# conditional code goes here:
 
 if (age >= 21) {
   print("I got in for the night!")
    (possible_codeword == real_codeword) {
       print("I need to get a good spot!")
   }  {
       print("Let's save for AV equipment next year.") 
   }

else if (age >= 18 & current_hour < 11) {
  print("I can stay until 11!")
}

else {
  print("I guess I'll go somewhere else.")
}