Question: 1 . Introduction to R Using Boolean Logic Write a piece of R code that you could use to teach a seven - year -

1. 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
Take note of several important aspects of this example: (1) It has a comment that explains a little bit of what is going on (all of the stuff 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)
Three logical operators:
& (And): if A and B are TRUE then and then only it is TRUE or 1, otherwise always FALSE
I (Or): if either A and B both are FALSE then it's FALSE otherwise it is always TRUE
!(Not): Basically flips the TRUE to FALSE, vice versa
2. Vectors
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)
Define a variable:
a <-150
Now that you have some data explore!
Step 1: Calculating means
Compute, using R, the average height (called mean in R).
Compute, using R, the average weight (called mean in R).
Calculate the length of the vector height and weight.
Calculate the sum of the heights.
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?
Step 2: Using max/min functions
Compute the max height, store the result in maxH.
Compute the min weight, store the results in minW.
Step 3: Vector math
Create a new vector, which is the weight +5(every person gained 5 pounds).
Compute the weight/height (weight divided by height) for each person, using the new weight just created.
Step 4: Using conditional if statements
*** The syntax for if else statement is: if (test_expression){statement1} else {statement2}
Hint: In R, one can do:
if (100<150)100 is less than 150 else 100 is greater than 150
Write the R code to test if max height is greater than 60(output yes or no).
Write the R code to test if min weight is greater than the variable a(output yes or no).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!