Exercises 2 - if else Solutions

Files Corresponding to Short Course Programming in R

Exercises 2 - if else Solutions

FizzBuzz is a common programming problem https://en.wikipedia.org/wiki/Fizz_buzz used to practice if statements and division.

For our version, create a vector test_vec of the numbers 1 to 50 (1:50). Then apply the following rules using dplyr::if_else statements to obtain the appropriate behavior:

Notes:

test_vec <- 1:50
library(dplyr)
if_else(test_vec %% 15 == 0, "FizzBuzz", 
       if_else(test_vec %% 3 == 0, "Fizz", 
              if_else(test_vec %% 5 ==0, "Buzz", as.character(test_vec))
              )
      )
##  [1] "1"        "2"        "Fizz"     "4"        "Buzz"     "Fizz"    
##  [7] "7"        "8"        "Fizz"     "Buzz"     "11"       "Fizz"    
## [13] "13"       "14"       "FizzBuzz" "16"       "17"       "Fizz"    
## [19] "19"       "Buzz"     "Fizz"     "22"       "23"       "Fizz"    
## [25] "Buzz"     "26"       "Fizz"     "28"       "29"       "FizzBuzz"
## [31] "31"       "32"       "Fizz"     "34"       "Buzz"     "Fizz"    
## [37] "37"       "38"       "Fizz"     "Buzz"     "41"       "Fizz"    
## [43] "43"       "44"       "FizzBuzz" "46"       "47"       "Fizz"    
## [49] "49"       "Buzz"