# Question A. Introduction to R Using Boolean Logic

# Write a piece of R code that you could use to teach a seven-year-old child about Boolean logic. If you have never encountered Boolean logic before (sometimes it is called Boolean algebra), you will have to look it up to discover the three essential operations (AND, OR, NOT) and how they work. In addition, you will have to discover the "operators" (i.e., the special punctuation marks) that R uses to represent Boolean operations.
# For example, here is a single line of code (and the response from the R-console in bold) that represents one of the two possible outcomes of the AND operator:

# This line shows the Boolean AND function at work

1 & 1
## [1] TRUE
# expected output: [1] TRUE

# Take note of several important aspects of this example: 
# (1) It has a comment that explains a little bit of what is going on (the line after the # character). 
# (2) It needs more comments if it is going to be helpful to a seven-year-old. 
# (3) More lines of code are needed to demonstrate the other outcome of AND, as well as all of the outcomes of OR and NOT. 
# Keeping your seven-year-old in mind, write and submit the rest of the code and comments.

# Then use these conditional statements within a if statement to printout the expected logic. For example:

# show the use of an ‘if’ statement - that 1 & 1 is true
if(1 & 1) print("1 & 1 is true") else print("error somewhere")
## [1] "1 & 1 is true"
# Note three logical operators:

# & (And): if A and B are TRUE then and then only it is TRUE or 1, otherwise always FALSE
# | (Or): if either A and B both are FALSE then it's FALSE otherwise it is always TRUE
# note that the Or character is a verticle line (usually on the same key as a slash "\"), not an I or an L
# ! (Not): Basically flips the TRUE to FALSE, vice versa

# Boolean Logic is a way for a computer to decide if something is true or false. 
# We can think of true as a "yes" or "1" and false as a "no" or "0". 
# There are three options, AND, OR, and NOT. 

# And (&) Requires both statements to be true for the result to be true. 
# For example, "I can have dinner and dessert"
#1 & 1 = 1
# Or (/) Requires one statement to be true for the result to be true. 
#0 / 1 = 1
# Not (!) Flips the value to the other. For exampe, "I can NOT (!) have dessert"
#0 / !1 = 0
#Lets print some examples and explain them

# Show the use of an if statement - that 1 & 1 is TRUE
if(1 & 1) print("1 & 1 is true") else print("error somewhere")
## [1] "1 & 1 is true"
# Show the use of an if statement - that 1 & 0 is FALSE
if(1 & 0) print("1 & 0 is true") else print("1 & 0 is false")
## [1] "1 & 0 is false"
# Here, we can witness, "I can have dinner and dessert" which is true if both are true. 
# If one is false, and you cannot have dinner but you can have dessert, this is false. 

# Show the use of an if statement - that 1 | 1 is TRUE
if(1 | 1) print("1 | 1 is true") else print("1 | 1 is false")
## [1] "1 | 1 is true"
# Show the use of an if statement - that 1 | 0 is TRUE
if(1 | 0) print("1 | 0 is true") else print("1 | 0 is false")
## [1] "1 | 0 is true"
# Show the use of an if statement - that 0 | 0 is FALSE
if(0 | 0) print("0 | 0 is true") else print("0 | 0 is false")
## [1] "0 | 0 is false"
#Here, we see that in an OR statement, I can have dinner or dessert is false only if both are false. 

# Show the use of NOT
if(!1) print("NOT 1 is true") else print("NOT 1 is false")
## [1] "NOT 1 is false"
if(!0) print("NOT 0 is true") else print("NOT 0 is false")
## [1] "NOT 0 is true"
# Finally, we can observe that the NOT Operator simply applies this inverse truth value. For example, "I can have dinner or I can not have dessert" 
# Question B. Vectors



# First, define the following vectors, which represent the weight and height of people on a particular team (in inches and pounds):

height <- c(59,60,61,58,67,72,70)
weight <- c(150,140,180,220,160,140,130)

# Second, define a variable:

a <- 150

# Now that you have some data, use these data to complete the following steps.

# Step 1: Calculating means

# a. Compute, using R, the average height (called mean in R).
mean(height)
## [1] 63.85714
# b. Compute, using R, the average weight (called mean in R).
mean(weight)
## [1] 160
# c. Calculate the length of the vector "height" and "weight."
length(height)
## [1] 7
length(weight)
## [1] 7
# d. Calculate the sum of the heights.
sum(height)
## [1] 447
# e. Compute the average of both height and weight, by dividing the sum (of the height or the width, as appropriate), by the length of the vector. How does this compare to the "mean" function?
sum(height) / length(height)
## [1] 63.85714
sum(weight) / length(weight)
## [1] 160
# Step 2: Using max/min functions
#run
# f. Compute the max height, store the result in "maxH."
maxH <- max(height) 
# g. Compute the min weight, store the results in "minW."
minW <- min(weight)

# Step 3: Vector math
#
# h. Create a new vector, which is the weight + 5 (every person gained 5 pounds).
newWeight <- weight + 5
newWeight
## [1] 155 145 185 225 165 145 135
# i. Compute the weight/height (weight divided by height) for each person, using the new weight just created.
newWeight / height
## [1] 2.627119 2.416667 3.032787 3.879310 2.462687 2.013889 1.928571
# Step 4: Using conditional if statements
if(maxH > 60) print("yes") else print("no")
## [1] "yes"
#
# Hint: In R, one can do:
if (100 < 150) "100 is less than 150" else "100 is greater than 150"
## [1] "100 is less than 150"
#
# j. Write the R code to test if max height is greater than 60 (output "yes" or "no").
# k. Write the R code to test if min weight is greater than the variable "a" (output "yes" or "no").
if(minW > a) print("yes") else print("no")
## [1] "no"