Question
Instruction Complete the following two questions. Use the knit function to export both the code and output into a html file. Upload the html file
Instruction Complete the following two questions. Use the knit function to export both the code and output into a html file. Upload the html file to the Canvas Assignment. Q1 - data frame Recall the example with cars2 data. Please review the example code below and study the use the "tapply" function. The tapply function is also explained in the RFIN book (page 88). {r} tall.packages("fBasics") ## if you have not installed "fBasics" package, install it on your local machine. # ins library("fBasics") #load the package cfr = cars2 print(head(cfr, 5)) # list all the cars made in US cfr1 <- subset(cfr, Country == "USA") # order the US-made cars by their gas mileage cfr1r <- cfr1[order(cfr1$Mileage, decreasing = FALSE), ] print(with(cfr1r, data.frame(rowNames, Country, Mileage))) # find the most gas efficienty US-made car cfr1m = subset(cfr1, Mileage == min(Mileage)) print(cfr1m[, c("rowNames", "Country", "Mileage")]) # How many cars are included in this dataset from each country byCountry_count = with(cfr, tapply(Country, as.factor(Country), length)) # What is the average age mileage of the cars made by each country? from each country byCountry_avgMileage = with(cfr, tapply(Mileage, as.factor(Country), mean)) byCountry_report = data.frame( country = names(byCountry_count), count = byCountry_count, avgMileage = byCountry_avgMileage) print(byCountry_report) Complete the code in the following session {r} message("~~~~~~~~~~~~~~~ begin of qustion 1 ~~~~~~~~~~~~~~~~~~~~~~~ ") # list the max, min, average gas mileage by type of cars. # byType_maxMileage = ?? # byType_minMileage = ?? # byType_avgMileage = ?? # find the car (rowNames) in each type that has the minimum gas mileage # find the car (rowNames) in each type that has the maximum gas mileage # report both cares in data frame # byType_report = ?? message("end of qustion 1 ") Q2 - loan payment Study the following examples. Ex4-1: starting from now, you will save $100 each year (at the year end) for 10 years. With the compound interest rate of 5%, calculate the year-end balance for the next 10 years. {r} yfr <- data.frame(year = 1:10) yfr$rate = 0.05 yfr$beg_balance = NA yfr$save = rep(100, 10) yfr$interest = NA yfr$end_balance = NA for (i in 1:nrow(yfr)) { if (i == 1) { yfr$beg_balance[i] = 0 } else { yfr$beg_balance[i] = yfr$end_balance[i-1] } yfr$interest[i] <- yfr$beg_balance[i] * yfr$rate[i] yfr$end_balance[i] <- yfr$beg_balance[i] + yfr$save[i] + yfr$interest[i] } print(yfr) {r} # plot the account with(yfr, plot( x = year, y = end_balance, type = 'b', col = 'black', lwd = 2, pch = 1, xlab = "year", ylab = "amount", main = "year-end balance")) with(yfr, points( x = year, y = cumsum(save), type = 'b', pch = 2, col = 'blue', lwd = 2)) legend("topleft", legend = c("balance", "cumulative savings"), lwd = 2, pch = c(1,2), col = c("black", "blue")) Now let us work out an problem with loan payment.
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