Starting from:

$24.99

STAT240 Homework 1 Solution

Preliminaries
This file should be in STAT240/homework/hw01 on your local computer.
While you should create your answers in the .Rmd file, homework problems are better formatted and therefore easier to read in the .html file. We recommended frequently knitting and switching between the two as you read and solve problems.
Problem 1
Consider a vector a which has three numeric elements, a vector b which has two numeric elements, and a single number on its own, c .
For each of the following operations, write down how many elements long the output will be.
We encourage you to think through the question first and make an initial guess, then check your work in the console.
This command will return _ numeric element(s).
This command will return _ numeric element(s).
This command will return _ numeric element(s).
This command will return _ numeric element(s).
This command will return _ numeric element(s).
Problem 2

Problem 3
Compute the mean of firstFifty .
Then, create a new vector which is the result of subtracting that mean from each value in firstFifty .
Finally, take the sum of that resulting vector.
Let that sum be printed as output, do not save it as a variable.

Note: This result is not just a special case with these specific numbers, this process will return that sum for any vector!
Problem 4
Compute the standard deviation of firstFifty without using the sd command. Let this single number be printed as output.
To compute the standard deviation, execute the following steps in this order:
Compute the mean of firstFifty and subtract it from every element in firstFifty , as in the previous question.
Square every element in that vector.
Take the sum of that new squared vector; this should be close to 10,400.
Divide that number by (the length of the vector minus 1). You can obtain the length of firstFifty with length(firstFifty) . Don’t forget parentheses & order of operations!
Finally, take the square root ( sqrt() ) of the number you got from the previous step.
The mathematical notation for this is:

Problem 5
We have provided you below with a vector containing all of the prime numbers from 1 to 50.
# We have written this code for you, do not edit it primes = c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47)
Create a vector of logical elements - that is, TRUE s and FALSE s; which indicates whether each of the elements in firstFifty is a prime number.
Let this vector be printed as output, do not save it as a variable.

Problem 6

Copy a sentence from the help page here.
6.B. What do toupper and tolower do to non-alphabetic characters? (Hint: Try using the “Find in Topic” box in the top bar of the help page, or click on the help page and use Ctrl+F on Windows/Command+F on Mac!)
Copy a sentence from the help page here.
Problem 7
Create a dataframe using the tibble() command with five columns:
The first column should be called char , and contain the 26 values of letters ,
The second column should be called pos , and contain the whole number values 1, 2, …, 25, 26.
The third column should be called isVowel , and should contain 26 logical values according to whether char in that row is one of “a”, “e”, “i”, “o”, or “u” or not. We won’t consider “y” as a vowel for simplicity.
The fourth column should be called isPrime , and should contain 26 logical values according to whether pos in that row is a prime number (or not).
The fifth column should be called vowelAndPrime , and should contain 26 logical values; TRUE when that row has both a vowel and a prime number, and FALSE otherwise. Hint: Use & !
Save this dataframe in a new variable called alphanumeric .
# Write your code here!
Problem 8

Looking at this output, Which vowel(s) are in a prime number position? In other words, in which rows is vowelAndPrime TRUE?
List the vowels that are in a prime number position here.
Submission
Once you have finished all the problems above, knit this file and submit BOTH the .Rmd and .html files on Canvas!

More products