Question
What to Turn In: A Compiled File of an R Script 1. Copy and paste all the questions in the lab to the R script
What to Turn In: A Compiled File of an R Script
1. Copy and paste all the questions in the lab to the R script file and save the R script file (1 point).
2. Comment out all the questions (1 point).
3. Submit the compiled file.
Follow this to learn how to compile your R script (1 point):
Follow the instruction of compiling here: Instructions for submitting lab assignments
If you have errors in compiling read this page: How to compile and Compiling Q&A Learning Objectives T
his exercise contributes to the development of problem solving skills.
After completing this exercise, students are able to manipulate vectors, and apply simple mathematical functions to vectors.
*** Write answers to each of the 11 questions in
2. Vectors.
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) 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started