R Markdown
file.Homework_4.RmdHomework_4.pdfR Markdown.R code inside R code chunks.R code chunk.Create a sequence of 20 numbers as follows: the first number is 5, and each number there after is found by multiplying the current number by 1.5 and then subtracting 1. Print the resulting sequence.
# Your code here
sequence <- numeric(20)
sequence[1]<- 5
for(x in 2:20){
sequence[x]<- 1.5* sequence[x-1]
}
print(sequence)
## [1] 5.00000 7.50000 11.25000 16.87500 25.31250 37.96875
## [7] 56.95312 85.42969 128.14453 192.21680 288.32520 432.48779
## [13] 648.73169 973.09753 1459.64630 2189.46945 3284.20418 4926.30627
## [19] 7389.45940 11084.18910
Suppose you have the following vector of probabilities, each of which measures the probability a person has a particular disease:
0.25, 0.83, 0.76, 0.25, 0.33, 0.51, 0.67, 0.50, 0.54, 0.75, 0.71, 0.03, 0.38, 0.64, and 0.19.
Create a new variable which takes value:
1 whenever the person is predicted to have the disease (probability ≥ 0.5).
0 whenever the person is predicted not to have the disease (probability < 0.5).
Do this two different ways:
First, using a loop.
Second, using the ifelse() function.
Print the resulting vectors. They should contain the exact same values.
# Your code here
p <- c(0.25,0.83,0.76, 0.25, 0.33, 0.51, 0.67, 0.50, 0.54, 0.75, 0.71, 0.03, 0.38, 0.64,0.19)
prd <- numeric(length(p))
for (i in 1: length(p)) {
if (p[i] >= 0.5 ){
prd[i] <- 1
}else{
prd[i] <- 0
}
}
print(prd)
## [1] 0 1 1 0 0 1 1 1 1 1 1 0 0 1 0
prdfelse <- ifelse(prd >= 0.5, 1, 0)
print(prdfelse)
## [1] 0 1 1 0 0 1 1 1 1 1 1 0 0 1 0
In this problem you will work with the Cars2020 dataset,
which you downloaded from
http://www.lock5stat.com/datapage3e.html and used on
Homework 3. Please work with the CSV again here.
Create a new variable that classifies the fuel efficiencies of the cars in the dataset based on the following criteria:
Make sure the dataset contains the new variable, and then print only the following for every car in the dataset:
the make and model of the car and
the classified fuel efficiency.
#install.packages("Lock5Data")
install.packages("Lock5Data", repos = "https://cloud.r-project.org")
## Installing package into 'C:/Users/david/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## package 'Lock5Data' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\david\AppData\Local\Temp\RtmpM9I4tE\downloaded_packages
library(Lock5Data)
## Warning: package 'Lock5Data' was built under R version 4.5.2
data("Cars2020")
Cars2020$fuel_efficiency <- ifelse(
Cars2020$HwyMPG < 20, "poor",
ifelse(Cars2020$HwyMPG <= 25, "acceptable",
ifelse(Cars2020$HwyMPG <= 35, "good", "great"))
)
result <- data.frame(
Make = Cars2020$Make,
Model = Cars2020$Model,
FuelEfficiency = Cars2020$fuel_efficiency
)
Use a loop to fill a 6 × 8 matrix (six rows and eight columns) as follows:
Print the resulting matrix.
Mat <- matrix(0, nrow = 6, ncol = 8)
for (i in 1:6) {
for (j in 1:8){
if ((i+ j) %% 2 == 0){
Mat[i,j] <- i * j
}else {
Mat[i,j]<- i+j +3
}
}
}
print(Mat)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1 6 3 8 5 10 7 12
## [2,] 6 4 8 8 10 12 12 16
## [3,] 3 8 9 10 15 12 21 14
## [4,] 8 8 10 16 12 24 14 32
## [5,] 5 10 15 12 25 14 35 16
## [6,] 10 12 12 24 14 36 16 48
Suppose I taught a course last semester that consisted of:
Homework (35%)
Midterm 1 (20%)
Midterm 2 (20%)
Final exam (25%)
Final letter grades are assigned as follows:
The grades for the students in the class can be found in the CourseGrades dataset on Blackboard.
Determine the letter grade each student earned in the course. Add that letter grade as a new variable to the data frame, and print the final data frame.
grades <- read.csv("CourseGrades.csv", header = TRUE)
#grades
grades$FinalScore <- 0.35 * grades$HW_Avg +
0.20 * grades$Exam1 +
0.20 * grades$Exam2 +
0.25 * grades$FinalExam
grades$LetterGrade <- ifelse(grades$FinalScore >= 90, "A",
ifelse(grades$FinalScore >= 80, "B",
ifelse(grades$FinalScore >= 70, "C",
ifelse(grades$FinalScore >= 60, "D", "F"))))
print(grades)
## Student HW_Avg Exam1 Exam2 FinalExam FinalScore LetterGrade
## 1 Bill 79.62 80 77 76 78.2670 C
## 2 Cameron 86.11 83 83 95 87.0885 B
## 3 Abby 94.53 94 92 86 91.7855 A
## 4 Will 78.22 77 76 96 81.9770 B
## 5 Janet 84.52 85 83 80 83.1820 B
## 6 Chris 85.79 85 83 81 83.8765 B
## 7 Laura 89.25 90 85 81 86.4875 B
## 8 Patricia 83.56 81 80 74 79.9460 C
## 9 George 96.91 97 92 76 90.7185 A
## 10 Joseph 84.17 85 80 96 86.4595 B
## 11 Diamond 87.51 87 84 78 84.3285 B
## 12 Jill 90.89 87 88 91 89.5615 B
## 13 T.J. 82.64 82 79 75 79.8740 C
## 14 Amy 78.76 77 74 68 74.7660 C
Suppose a friend of yours is currently a second-semester senior, and her GPAs for the previous semesters (starting first semester freshman year) are:
4.00, 3.50, 3.67, 3.33, 2.67, 3.42, and 3.56
Assuming she took the same number of credits each semester, write a loop that calculates and stores her cumulative GPA after each semester.
Then print the resulting vector of cumulative GPAs.
gpa <- c(4.00, 3.50, 3.67, 3.33, 2.67, 3.42, 3.56)
cgpa<- numeric(length(gpa))
for (i in 1:length(gpa)) {
cgpa[i] <- mean(gpa[1:i])
}
print(cgpa)
## [1] 4.000000 3.750000 3.723333 3.625000 3.434000 3.431667 3.450000
Returning to Problem 6, suppose your friend was on an academic scholarship that requires the student to maintain a cumulative GPA of at least 3.6 in order to continue receiving financial support the following semester.
If at any point the student’s cumulative GPA drops below 3.6, they lose the scholarship permanently.
Write a while loop that counts the number of semesters your friend received financial support during her college career. Print that number.
Assume your friend received the scholarship starting first semester freshman year.
gpa <- c(4.00, 3.50, 3.67, 3.33, 2.67, 3.42, 3.56)
i <- 1
count <- 0
currnt_sum <- 0
while (i <= length(gpa)) {
currnt_sum <- currnt_sum + gpa[i]
cgpa <- currnt_sum/ i
if (cgpa < 3.6){
break
}
count<- count + 1
i <- i + 1
}
print(count)
## [1] 4
The Fibonacci sequence is defined as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
Each number after the first two is the sum of the two preceding numbers.
If you are not familiar with the Fibonacci sequence or how each number is generated, please search for it online. It is a fascinating sequence that appears frequently in mathematics and nature!
Write an R script that calculates and prints the first 40 numbers in the Fibonacci sequence.
You may start with the first two numbers (0 and 1). Use a loop or any method you prefer to generate the remaining numbers.
FS <- numeric(40)
FS[1]<-0
FS[2]<- 1
for (i in 3:40){
FS [i] <- FS[i-1] + FS[i-2]
}
print(FS)
## [1] 0 1 1 2 3 5 8 13
## [9] 21 34 55 89 144 233 377 610
## [17] 987 1597 2584 4181 6765 10946 17711 28657
## [25] 46368 75025 121393 196418 317811 514229 832040 1346269
## [33] 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986