f.getNA <- function(x,print=T){
result <- which(is.na(x))
if(print){
print(paste(length(result),'NA values are found'))
}
return(result)
}STAT120 - Statistical Computing in R
Homework 8
Part I: Writing functions
In this part, you will be asked to write R functions. You will be using these functions in Part II. Refer to Q1 of Part II to get a sense of how these functions should work. (We separate the two processes of writing and using functions here because this is what you would typically do: you would create a separate R file only for functions and then do the rest of the coding after “loading” the functions you have written.)
- Write a function, called f.getNA, that takes a vector as its argument, and returns the index of NA values in the vector. In addition, add a print option, defaulted to TRUE. If TRUE, the function will print how many NA values are found; if FALSE, nothing will be printed.
Show solution
- Write a function, called f.max2, that takes two vectors of the same length, calculates the (element-wise) sum of the two vectors, and returns the index of the element with the largest sum. If there is a tie, it should return all of the indices with the largest sum.
Show solution
f.max2 <- function(x,y){
#x <- HollywoodMovies$DomesticBO
#y <- HollywoodMovies$WorldwideBO
#x <- c(90,100,100,90,50,80)
#y <- c(100,90,50,40,55,80)
#return(which.max(x+y))
return(which((x+y)==max(x+y,na.rm = T)))
}- Write a function, called f.topdiff, that takes two vectors of the same length and calculates the (element-wise) difference of the two vectors. The difference should be calculated in absolute value (i.e., difference between 1 and 5 is 4, difference between 5 and 1 is also 4; hint: you can use the function abs()). The function takes another argument, top (default to 1). If top is 3 (say), the function will return the indices of the top 3 biggest differences (don’t worry about handling ties). If top is an invalid value (like -3, 1.2, or a number larger than allowed), print an error message and return nothing.
Show solution
f.topdiff <- function(x,y,top=1){
#x <- HollywoodMovies$DomesticBO
#y <- HollywoodMovies$WorldwideBO
# top = 2
#x <- c(90,100,100,90,50,80)
#y <- c(100,90,50,40,55,80)
diff <- abs(x-y)
if(top %in% c(1:length(diff))){
result <- data.frame(diff, index=c(1:length(diff))) %>%
arrange(desc(diff)) %>%
slice(1:top) %>%
pull(index)
### OR ###
# result <- order(diff,decreasing=T)[1:top]
return(result)
}else{
print('Invalid top value')
}
}- Write a function, called f.plotn, that takes two vectors of the same length and generate a minimalistic scatterplot. Use plot() to do the scatterplot and set type to line (type=‘l’). Remove the x and y labels and the x and y axis. Set the line width (lwd) to 3. Add an argument to the function, color (default to ``black”), which controls the color of the scatterplot. This function only generates the plot and it does not need to have a return value.
Show solution
f.plotn <- function(x,y,color='black'){
plot(x,y,type ='l',col=color,xlab=NA,ylab=NA,xaxt='n',yaxt='n',lwd=3)
}Part II: Using functions
- We want to test the functions in Part II. Run the code given below and check the output. If you see anything that is not working correctly, you need to go back to modify your function.
vec1 <- c(10,11,15,NA,24,NA,15)
vec2 <- seq(from=-2,to=2,by=0.01)
exam1 <- c(90,100,100,90,50,80)
exam2 <- c(100,90,50,40,55,80)
f.getNA(vec1,print=T)
f.getNA(vec1,print=F)
f.getNA(vec2)
f.max2(exam1,exam2) #student(s) with the highest exam 1 and exam 2 grades combined
f.topdiff(exam1,exam2,top=2) #student(s) with the largest difference between exam 1 and exam 2
f.topdiff(exam1,exam2,top=12)
f.plotn(vec2,vec2^2)
f.plotn(vec2,sin(vec2),color='royalblue')- Look at the HollywoodMovies dataset in the Lock5Data package. Use f.max2 to find which movie(s) has the highest domestic box office and foreign box office values combined (DomesticBO, ForeignBO). Print the information of those movies. Note that you might need to adjust your function to deal with NA values.
Show solution
HollywoodMovies %>% slice(f.max2(DomesticBO,ForeignBO))- Use f.topdiff to find the top 5 movie(s) with the largest discrepancy between domestic box office and foreign box office. Print the information of those movies. Note that you might need to adjust your function to deal with NA values.
Show solution
HollywoodMovies %>% slice(f.topdiff(DomesticBO,ForeignBO,top=5))