Browsing resource, all submissions are temporary.
a. Take a look at the 'chickwts' dataset that comes with R. The data can be loaded with the command data(chickwts). A description of the dataset can be found by running ?chickwts.
data(chickwts)
?chickwts
A data frame chickwts is loaded to your workspace after running data(chickwts). In this dataset, what is the mean of the chick weight for the chicks that were fed with "soybean" and "linseed"? Give your answers to at least 4 significant figures.
chickwts
b. The pbirthday(n) function returns the probability that at least 2 people share the same birthday among n randomly chosen people. The input n can only be a single integer. It won't work if you try, for example, pbirthday(10:40). Suppose I want to find the probabilities for numbers from 10 to 40 and store the results to a numeric vector pb of length 31. Which of the following commands could I use? (Select all that apply) pb <- lapply(10:40, pbirthday) pb <- vapply(10:40, pbirthday, numeric(1)) pb <- mapply(10:40, pbirthday) pb <- sapply(pbirthday, 10:40) pb <- sapply(10:40, pbirthday) pb <- lapply(pbirthday, 10:40)
pbirthday(n)
pbirthday(10:40)
pb
c. Continuing with the question in part (b). After assigning values to the numeric vector pb, I add names to pb by the command
names(pb) <- 10:40
Suppose now I want to look at the values of the probabilities for n=20, 30, 35 and 40. Which of the following commands could I use? (Select all that apply) pb[11,21,26,31] pb[c(11,21,26,31)] pb[20,30,35,40] pb[c("20","30","35","40")] pb["20","30","35","40"] pb[c(20,30,35,40)]
d. In Example 3 of Week 4's notes, a data frame named MonthlyAvg is created to store the monthly average of the maximum, minimum, and average daily temperature for each month in the years 2003-2016 in Champaign, Illinois. The data are then further used to compute the monthly averages over the 14 years by looping over the 12 months. For example, to calculate the monthly average of the maximum daily temperature over those 14 years, the code used in the notes is similar to the following.
MonthlyAvg
Tmax_avg <- rep(NA,12) for (i in 1:12) { Tmax_avg[i] <- mean(MonthlyAvg$Tmax[MonthlyAvg$Month==i]) }
Which of the following commands can be used to replace the code above? (Select all that apply) Tmax_avg <- tapply(MonthlyAvg$Tmax, 1:12, mean) Tmax_avg <- tapply(MonthlyAvg$Tmax, MonthlyAvg$Month, mean) Tmax_avg <- vapply(1:12, mean(MonthlyAvg$Tmax), numeric(1)) Tmax_avg <- lapply(list(MonthlyAvg$Tmax,1:12), mean) Tmax_avg <- sapply(list(MonthlyAvg$Tmax, MonthlyAvg$Month), mean) Tmax_avg <- mapply(mean, MonthlyAvg$Tmax, MonthlyAvg$Month) Tmax_avg <- sapply(split(MonthlyAvg$Tmax, MonthlyAvg$Month), mean)
Tmax_avg <- tapply(MonthlyAvg$Tmax, 1:12, mean)
Tmax_avg <- tapply(MonthlyAvg$Tmax, MonthlyAvg$Month, mean)
Tmax_avg <- vapply(1:12, mean(MonthlyAvg$Tmax), numeric(1))
Tmax_avg <- lapply(list(MonthlyAvg$Tmax,1:12), mean)
Tmax_avg <- sapply(list(MonthlyAvg$Tmax, MonthlyAvg$Month), mean)
Tmax_avg <- mapply(mean, MonthlyAvg$Tmax, MonthlyAvg$Month)
Tmax_avg <- sapply(split(MonthlyAvg$Tmax, MonthlyAvg$Month), mean)