CS计算机代考程序代写 GBA 464: PROGRAMMING FOR ANALYTICS

GBA 464: PROGRAMMING FOR ANALYTICS
FINAL EXAM (Part I)

October 18, 2020

Instructions

(1) The exam is divided into two parts and the total duration is 140 minutes. This is the first part of
the exam and the time limit for this part is 60 minutes.
(2) This part will take 60 points out of 100 points for the entire exam. There are a total of 10
questions. Five of them are multiple choice questions, and five are coding questions.
(3) You have access to all lecture notes and your R/RStudio software.
(4) Keep in mind that there are partial credits: in any case, giving your best attempt is better than
leaving a question blank. But, also keep in mind that everything you write counts: so only write
down answers that you think are most correct.
(5) Important: You have to complete the exam all on your own. That is, turn off email and social
network software on your computer. Turn off your cellphone. During the exam, you have to remain
in the Zoom room with your webcam on and your mic on (i.e., unmuted). Turn off your speaker
volume to not be disturbed by others.
(6) Good Luck!

1

[Question 1]. Which word best describes the following objects: c(), lm(), mean(), aggregate()? [4
pts]
(A) Variables
(B) Expressions
(C) Loops
(D) Functions

[Question 2]. If I run

x <- "2012-02-28" What is the data type of x? [4 pts] (A) Date (B) Numeric (C) Character (D) Factor [Question 3]. Which of the following is the local value of b (in the line indicated), if we call f(2)? [4 pts] b <- 5 f <- function(a, b = a^2) { b <- a^2 + 5*b # what is the value of b in this line? return(a^2) } f(2) (A) 4 (B) 5 (C) 24 2 (D) 25 [Question 4]. ’df’ is the following data frame: ## id name gender income age ## 1 1 Ed male 2000 20 ## 2 2 Liz female 3000 20 ## 3 3 Alex male 4000 20 ## 4 4 Max male 5000 25 ## 5 5 Fiora female 6000 25 ## 6 6 Emma female 7000 25 You would like to take average income by gender and age, i.e. you wanted to generate the following result: ## gender age income ## 1 female 20 3000 ## 2 male 20 3000 ## 3 female 25 6500 ## 4 male 25 5000 Which of the following cannot achieve this task? [4 pts] # A) cast(data = df, formula = gender + age ~ ., fun.aggregate = mean, value = "income") # B) melt(df, id = c("gender", "age")) # C) aggregate(income ~ gender + age, df, FUN = mean) # D) tapply(X = df$income, INDEX = list(gender = df$gender, age = df$age), FUN = mean) 3 [Question 5]. ’x’ is a vector c(-2, -1, 0, 1, 2). Which section of code gives the following result? [4 pts] ## [1] -2 -1 0 1 2 (A) if (x <= 0) { y <- x } else { y <- -x } y (B) y <- abs(x) y (C) y <- ifelse(x <= 0, x, -x) y (D) ifelse(x <= 0, y <- x, y <- -x) y 4 [Question 6]. Combine a vector ’a’ which is (1, 2, 3), with a vector ’b’ which is ("4", "five", "SIX"), into a vector ’v’ of length 6. Please write down your code. [8 pts] [Question 7]. ’df’ is the following data frame: ## id name gender income age ## 1 1 Ed male 2000 20 ## 2 2 Liz female 3000 20 ## 3 3 Alex male 4000 20 ## 4 4 Max male 5000 25 ## 5 5 Fiora female 6000 25 ## 6 6 Emma female 7000 25 Create a logical column in the data frame ’df’, called ’is_female’, that is TRUE when the person is female and FALSE otherwise. Write down your code. [8 pts] [Question 8]. We have a vector ’a’: ## [1] 1 2 3 4 5 6 7 8 9 Write one line of code to convert ’a’ into a 3x3 matrix. [8 pts] 5 ## [,1] [,2] [,3] ## [1,] 1 4 7 ## [2,] 2 5 8 ## [3,] 3 6 9 [Question 9]. For any positive integer k, create a list with length k, where the first element is an integer vector from k to 1, the second element is an integer vector from k-1 to 1, ..., and the kth element is the integer 1. For example, when k = 4, you should generate the following list. Write your code with a general integer k. [8 pts] ## [[1]] ## [1] 4 3 2 1 ## ## [[2]] ## [1] 3 2 1 ## ## [[3]] ## [1] 2 1 ## ## [[4]] ## [1] 1 6 [Question 10]. This is part of the lyrics of Eminem’s old song ’lose yourself’. Let’s call the following (scalar) character string ’lyrics’.1 ## His palms are sweaty, knees weak, arms are heavy, ## There's vomit on his sweater already, ## mom's spaghetti. He's nervous, ## but on the surface he looks calm and ready, ## To drop bombs, but he keeps on forgettin'. Write some code using regular expressions to generate the following vector. [8 pts] ## [1] "sweaty," "heavy," "already," "ready," 1You can assume that every word is followed by a space. 7