Professional Documents
Culture Documents
R Interview Questions
r-interview.md Raw
1.) If I have a data.frame df <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6), c(7, 8, 9)) ...
Answers: (a) df[[2]] or df$b , (b) df[[1]][[1]] or df$a[[1]] , (c) df[[2]][[2]] or df$b[[2]] , (d) 7 8 9, (e) 1 4 7, (f) 5.
Answer: A dataframe can contain heterogenous inputs and a matrix cannot. (You can have a dataframe of characters,
integers, and even other dataframes, but you can't do that with a matrix -- a matrix must be all the same type.)
3a.) If I concatenate a number and a character together, what will the class of the resulting vector be?
4.) What is the difference between sapply and lapply ? When should you use one versus the other? Bonus: When should
you use vapply ?
Answer: Use lapply when you want the output to be a list, and sapply when you want the output to be a vector or a
dataframe. Generally vapply is preferred over sapply because you can specify the output type of vapply (but not
sapply ). The drawback is vapply is more verbose and harder to use.
Answer: seq(4) produces a vector from 1 to 4 ( c(1, 2, 3, 4) ), whereas seq_along(4) produces a vector of length(4) , or
1 ( c(1) ).
y <- 5
f <- function(x) { y <- 2; y^2 + g(x) }
g <- function(x) { x + y }
Why?
Answer: 12. In f(3) , y is 2, so y^2 is 4. When evaluating g(3) , y is the globally scoped y (5) instead of the y that is
locally scoped to f , so g(3) evaluates to 3 + 5 or 8. The rest is just 4 + 8, or 12.
7.) I want to know all the values in c(1, 4, 5, 9, 10) that are not in c(1, 5, 10, 11, 13) . How do I do that with one built-
in function in R? How could I do it if that function didn't exist?
Answer: setdiff(c(1, 4, 5, 9, 10), c(1, 5, 10, 11, 13)) and c(1, 4, 5, 9, 10)[!c(1, 4, 5, 9, 10) %in% c(1, 5, 10,
11, 13) .
8.) Can you write me a function in R that replaces all missing values of a vector with the mean of that vector?
Answer:
9.) How do you test R code? Can you write a test for the function you wrote in #6?
Answer: You can use Hadley's testthat package. A test might look like this:
fn(a, b, c, d, e) a + b * c - d / e
How do I call fn on the vector c(1, 2, 3, 4, 5) so that I get the same result as fn(1, 2, 3, 4, 5) ? (No need to tell me
the result, just how to do it.)
11.)
Why does the dplyr package get loaded and not ggplot2?
Answer: deparse(substitute(dplyr))
12.)
What is the value of fn(3) ? Can you explain what is happening at each step?
Answer:
This Reduce call is wonky, taking three arguments. A three argument Reduce call will initialize at the third argument, which
is 3.
The inner function, function(y, w) w(y) is meant to take an argument and a function and apply that function to the
argument. Luckily for us, we have some functions to apply.
We then take the value 4 and apply the second function. 4 * 4 = 16.
informative
Very good set of questions. Can you expand on this set of questions and answers?
It doesn't look that sapply returns data.frame, nevertheless you can convert into it easily.
is.data.frame(
sapply(data.frame(seq(4),seq(4)),function(x){return(x)})
)
[1] FALSE
Listening on http://127.0.0.1:5553
Warning in file(file, "r") :
cannot open file 'C:/Users/admin/Documents/positive-words.txt': No such file or directory
Warning: Error in file: cannot open the connection
Stack trace (innermost first):
39: file
38: scan
37: server [C:\Users\admin\Documents\rr/server.R#78]
1: runApp
Error in file(file, "r") : cannot open the connection
and my r studio version is 3.4.3 please help to this error and i will gave correct path but generate same error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
© 2020 GitHub, Inc. Terms Privacy Security Status Help Contact GitHub Pricing API Training Blog About