Вопросы и ответы по R: часть 3

Вопросы и ответы по программированию на R (для собеседований). Полный список вопросов доступен здесь.

Часть 3 (14 вопросов)

1) If I have a data.frame df <- data.frame(a = c(1, 2, 3), b = c(4, 5, 6), c(7, 8, 9))

1a.) How do I select the c(4, 5, 6)?

1b.) How do I select the 1?

1c.) How do I select the 5?

1d.) What is df[, 3]?

1e.) What is df[1,]?

1f.) What is df[2, 2]?

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.

2) What is the difference between a matrix and a dataframe?

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.)

3) If I concatenate a number and a character together, what will the class of the resulting vector be?

Answer: character

4) What if I concatenate a number and a logical?

Answer: number

5) What if I concatenate a number and NA?

Answer: number

6) 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.

7) What is the difference between seq(4) and seq_along(4)?

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)).

8) What is f(3) where:


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.

9) 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).

10) Can you write me a function in R that replaces all missing values of a vector with the mean of that vector?


11) How do you test R code? Can you write a test for the function you wrote in #8?

Answer: You can use Hadley’s testthat package. A test might look like this:

12) Say I have…

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.)

Answer: do.call(fn, as.list(c(1, 2, 3, 4, 5)))


Why does the dplyr package get loaded and not ggplot2?

Answer: deparse(substitute(dplyr))


What is the value of fn(3)? Can you explain what is happening at each step?


Best seen in steps.

fn(3) requires mystery_method to be evaluated first.

mystery_method(c(function(x) x + 1, function(x) x * x)) evaluates to…

Now, we can see the 3 in fn(3) is supposed to be z, giving us…

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.

That means we intialize at 3 and apply the first function, function(x) x + 1. 3 + 1 = 4.

We then take the value 4 and apply the second function. 4 * 4 = 16.


Часть 1

Часть 2

Data Scientist # 1

Машинное обучение, большие данные, наука о данных, анализ данных, цифровой маркетинг, искусственный интеллект, нейронные сети, глубокое обучение, data science, data scientist, machine learning, artificial intelligence, big data, deep learning

Данные — новый актив!

Эффективно управлять можно только тем, что можно измерить.
Copyright © 2016-2021 Data Scientist. Все права защищены.