Choices and Iteration
If my bike is working
then I ride my bike to campus.
If my bike is not working and I have enough time
then I ride the bus to campus.
If my bike is not working and I don’t have enough time
then I take a cab.
rnorm() will generate a random number from a Normal distribution with mean 0 and SD 1.
“if then” statement: executes a block of code if a condition is met.
“if else” statement: executes one block of code if a condition is met and a different block of code if it is not met.
If my bike is working
then I ride my bike to campus.
If my bike is not working and I have enough time
then I ride the bus to campus.
If my bike is not working and I don’t have enough time
then I take a cab.
Using the R objects bike_working and enough_time, both logical vectors of length 1, write this decision tree in R using nested if-then statements and print().
02:30
[1] "x is positive :)"
[1] "x is positive :)"
[1] "element 1 is positive :)"
[1] "element 2 is positive :)"
[1] "0.0955192748637143 is positive :)"
[1] "0.863686410991415 is positive :)"
ifelse()ifelse(): A vectorized if-else statement.
