Put your R code and explanations below each question in this markdown file and submit the knitted html file to Canvas.

President ages

  1. Create a vector ages containing the age (at inauguration) of the ten most recent Presidents. Using the names function add last names to the values of this vector. Create another vector year containing the year at inauguration of these ten Presidents.
    • I created the vector ‘ages’ by manually combining all of the ages. Then I used the name function on ages to correlate the last name to their age. Finally, I created a vector ‘year’ to combine all of inauguration years of the presidents and printed the ‘ages’ and ‘year’ vector.
ages <- c(78,70,47,54,46,64,69,52,61,56)
names(ages) <- c('Biden','Trump','Obama','Bush','Clinton','Bush','Reagan','Carter','Ford','Nixon')
year <- c(2021,2017,2009,2001,1993,1989,1981,1977,1974,1969)
print(ages)
##   Biden   Trump   Obama    Bush Clinton    Bush  Reagan  Carter    Ford   Nixon 
##      78      70      47      54      46      64      69      52      61      56
print(year)
##  [1] 2021 2017 2009 2001 1993 1989 1981 1977 1974 1969
  1. Find the mean, min, and maximum age of these 10 Presidents.
    • I used the mean(), min(), and max() functions to find the mean, minimum and maximum ages for the presidents. I then stored them each in their own variable. I combined each of these variables into a final vector and used the name() function on this vector to correlate the number to the name. I finally printed out the final vector.
meanPres <- mean(ages)
minPres <- min(ages)
maxPres <- max(ages)

finalVec <- c(meanPres,minPres,maxPres)
names(finalVec) <- c('Mean','Min','Max')

print(finalVec)
## Mean  Min  Max 
## 59.7 46.0 78.0
  1. Using logical operations, create a new vector age_young with the ages of the Presidents younger than 50.
    • I stored all of the presidents ages that were younger than 50 into a separate variable by using the ‘ages’ vector and only selecting the ones whose age in the vector is younger than 50 by using a ‘<’. I then printed that vector.
age_young <- ages[ages<50]

print(age_young)
##   Obama Clinton 
##      47      46
  1. Create a vector age_after_1975of the ages of the Presidents inaugurated after 1975.
    • I used the ‘year’ vector inside of the ‘ages’ vector ti find the index at which the presidents were inaugurated after 1975 by doing ‘year > 1975’ and stored it in a new vector. I then printed this vector.
age_after_1975 <- ages[year > 1975]

print(age_after_1975)
##   Biden   Trump   Obama    Bush Clinton    Bush  Reagan  Carter 
##      78      70      47      54      46      64      69      52
  1. Using the plot() function, plot the inauguration year against the age for these 10 Presidents (x=age, y=ear). Do you see any pattern in this graph? Please provide your observation in bold.
    • There is a trend line sloping down. Meaning that as we go farther back in time the ages of the presidents tend to be older, and as we move forward in time the presidents tend to be younger. But there are a few outlines that do not follow this pattern.
plot(ages,year,type= "p")

Writing hand and gender

For a class of 20 students, the following vectors store their gender (F or M) and writing hand (L or R).

gender <- c("M","F","F","M","F","M","F","F","M","F","M","F","M","F","F","F","M","F","F","F")
hand <- c("R","R","L","R","R","R","R","L","R","R","R","R","R","R","L","L","L","R","R","R")
  1. Using table() fucntion, construct and display a frequency table of writing hand. Type ?table in console and press Enter to see how table() works.
    • I used the table() function with the vector ‘hand’ to create a table that separates the number of left hands from the number of right hands and stored it in a variable. I then printed this table out.
handFreq <- table(hand)

print(handFreq)
## hand
##  L  R 
##  5 15
  1. Using barplot() function, construct a bar plot of these writing hand frequencies. Type ?barplot() in console and press Enter to see how barplot() works.
    • I used the variable that I stored the table into and used the barplot() function to visually show the number of left hands from the number of right hands.
barplot(handFreq)

  1. Using logical operations, create a vector of the writing hand of the women and another vector of the writing hand the men.
    • I used the ‘hand’ vector and found the index in which a ‘F’ was present and stored it into a vector by using the ‘==’ operator and the ‘gender’ vector together. I did the same thing for ‘M’. I then printed out these two vectors.
  womenVec <- hand[gender == 'F']

  menVec <- hand[gender == 'M']
  
  print("Women:")
## [1] "Women:"
  print(womenVec)
##  [1] "R" "L" "R" "R" "L" "R" "R" "R" "L" "L" "R" "R" "R"
  print("Men:")
## [1] "Men:"
  print(menVec)
## [1] "R" "R" "R" "R" "R" "R" "L"

Functions and vector operations

  1. Write a function called from_F_C() which takes the temperature value in degrees Fahrenheit as an input and outputs its corresponding value in degrees Celsius. Test your function for 32F and 95F.
    • Inside the ‘from_F_C()’ function I returned the equation of converting from Fahrenheit to Celsius. I then called called the function and printed on two different values,
from_F_C <- function(tempF){
  return((tempF-32) * (5/9))
}

print(from_F_C(32))
## [1] 0
print(from_F_C(95))
## [1] 35
  1. It is well known that \(\sin(1)\) (the value of sine function at 1) can be approximated by the sum \[ 1 - \frac{1}{3!} + \frac{1}{5!} - \frac{1}{7!} + … + (-1)^n\frac{1}{(2n+1)!} \] where \(n\) is a non-negative integer and \(k! = 1 \cdot 2\cdots k\) (\(k\) factorial). Let n be in the range 0,…,7. Using the for loop and the conditional if, find the smallest value of \(n\) such that \[ |Error| = |1 - \frac{1}{3!} + \frac{1}{5!} - \frac{1}{7!} + … + (-1)^n\frac{1}{(2n+1)!} - \sin(1)| < 0.0001 \] When the smallest such \(n\) is found, print it on the screen and use break command to exit the for loop. Also, using this \(n\), print your approximation of the value of \(\sin(1)\). Note: In R, \(\sin(1)\) is sin(1) and \(k!\) is factorial(k).
aprox <- 0
for(n in 0:7){
  currTerm <- (-1)^n / factorial(2*n+1)
  
  aprox <- aprox + currTerm
  
  error <- abs(aprox-sin(1))
  
  if(error < 0.0001){
    print("Smallest n is:")
    print(n)
    print("The approx of sin(1):")
    print(aprox)
    break
  }
}
## [1] "Smallest n is:"
## [1] 3
## [1] "The approx of sin(1):"
## [1] 0.8414683