In: Statistics and Probability
Q4. Suppose the character string “05/04/06” is assigned to an object, x. Which of the following R commands would convert x to a date object that represents May 4, 2006? (Check all that apply)
A. lubridate::mdy(x)
B. lubridate::mdy(x, format = “m/d/y”)
C. as.Date(x, format = ‘%m/%d/%y’)
D. as.Date(x, format = “m/d/y”)
E. as.Date(x, format = ‘m/d/y’)
F. as.Date(x)
Q5. Suppose you have a dataframe, df, with two datetime columns in them: firstPurchaseTime and lastPurchaseTime. These columns are a time stamp that measures time to the second. What would be the outcome for the following line of code?
df$timePassed = as.numeric(df$lastPurchaseTime – df$firstPurchaseTime)
A new column of data the represents the amount of time that has passed in terms of:
A. seconds
B. minutes
C. hours
D. days
E. years
Q6. Suppose you have a dataframe, df, with two datetime columns in them: firstPurchaseTime and lastPurchaseTime. These columns are a time stamp that measures time to the second. What would be the outcome for the following line of code?
boxplot(df$lastPurchaseTime)
A. Nothing, because datetime objects can’t be summarized like numeric values.
B. A box plot that represents the distribution of time stamps.
C. A box plot that represents the distribution of days of the week.
D. A box plot that represents the distribution of months of the year.
E. A box plot that represents the distribution of years.
Q7. What is the result of the following lines of code:
x <- c(‘Product 1’, ‘Product 1’, ‘Product 2’, ‘Product 2’, ‘Product 3’) plot(x)
A. A boxplot that represents the distribution of products.
B. A lineplot that represents the frequency of each product.
C. An error message because R doesn’t know how to aggregate character strings.
D. A histogram that represents the frequency of each product.
E. An error message because R doesn’t know how to aggregate date types.
Q8. One vector, v1, contains the values of 5 and 15. Another vector, v2, contains the values of 6 and NA. What is the result of the following line of code?
v1 < v2
A. A vector that contains one value, NA.
B. A vector that contains two values, TRUE and NA
C. A vector that contains two values, TRUE and FALSE
D. A vector that contains two values, NA and NA
E. A vector that contains one value, TRUE
Q9. df1 is a datframe object that has ten rows and four columns. Df2 is a dataframe object that has eight rows and four columns. The column names in each dataframe are identical. Which line of code will stack the rows from df2 below the rows of df1 in a new dataframe object,
A. df3? df3 <- cbind(df2, df1)
B. df3 <- cbind(df1, df2)
C.df3 <- rbind(df2, df1)
D.df3 <- rbind(df1, df2)
Q 10. What will show up in the RStudio console as a result of running the following lines of code?
for(i in 1:5){ print(sum(i+10)) }
A. The numbers 11, 23, 36, 50, and 65, each on a separate line.
B. The numbers 11, 23, 36, 50, and 65, all on the same line.
C. The numbers 11, 12, 13, 14, and 15, each on a separate line.
D. The numbers 11, 12, 13, 14, and 15, all on the same line.
Please don't hesitate to give a "thumbs up" for the answer in case the answer has helped you
Answering first question, as we are allowed to answer 1 question per post. Please post the other questions separately.
4.
We run the following commands in R session to check if they give us the expected output. First we need to install the lubridate package as this is not included in the base package.
# install packages for lubridate for running functions
installed.packages("lubridate")
# running library for lubridate
library('lubridate')
x <- "05/04/06"
a <- lubridate::mdy(x)
# a : "2006-05-04"
b <- lubridate::mdy(x, format = "m/d/y")
# this errors out
c <- as.Date(x, format = "%m/%d/%y")
# this gives the expected output
d <- as.Date(x, format = "m/d/y")
# returns NA
e <- as.Date(x, format = "m/d/y")
# returns NA
f <- as.Date(x)
# this returns "0005-04-06" , not correct output
Answer: Only A and C are correct