Put your R code and explanations below each question in this markdown
file and submit the knitted html file to Canvas.
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.
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
mean, min, and
maximum age of these 10 Presidents.
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
age_young
with the ages of the Presidents younger than 50.
age_young <- ages[ages<50]
print(age_young)
## Obama Clinton
## 47 46
age_after_1975of the ages of the
Presidents inaugurated after 1975.
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
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.
plot(ages,year,type= "p")
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")
table() fucntion, construct and display a
frequency table of writing hand. Type ?table in console and
press Enter to see how table() works.
handFreq <- table(hand)
print(handFreq)
## hand
## L R
## 5 15
barplot() function, construct a bar plot of these
writing hand frequencies. Type ?barplot() in console and
press Enter to see how barplot() works.
barplot(handFreq)
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"
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.
from_F_C <- function(tempF){
return((tempF-32) * (5/9))
}
print(from_F_C(32))
## [1] 0
print(from_F_C(95))
## [1] 35
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