4 хитрых вопроса по R

Может пригодиться на интервью.

Question 1 : Rotational multiplication

You have two vector defined as follows :

What is the value of the vector d, which is defined as follows :

> d <- a*b

Answer: 2 , 6 , 4

R language does vectorized operations. ‘a’ and ‘b’ are two vectors with different length. By process, R multiplies the first element of a with 1st element of b, than second element of a with that of b, and so on. But in this case, after the second multiplication R hits the end of vector “b”. In such cases R, starts with the first element of smaller vector till each element of longer vector is exhausted. The vectorized operation always leads to a vector of length equal to that of longer vector.

Question 2 : Scoping Rules

You need to understand the following code and answer a question based on this understanding.

What is the value of f(6)?

Answer: 22

If you answered anything other than 22, you probably need to refresh the lexical scoping in R. The function f(x) returns a value y^2 + g(x). y in this environment has been defined as 2 and g(x) from inside this function. The value of x is passed of function g as 6. Now comes the catch, what is the value of free variable y here? Unlike dynamic environment where the value is assumed from the parent environment, lexical scoping assumes the value of a variable from the environment where the function is defined. The function g(x) is defined in the global environment here, and hence the value of y is assumed to be 3. Therefore a value of 18 is returned from the function g(x). f(6) is finally returning as 22.

Question 3 : Summarizing at each factor

You have been assigned to check two race tracks. To complete this task you are expected to find the means of the total time taken by cars to cross the track. In the following data assignment, “b” is the vector of total time taken by different cars and “a” is the vector of track on which this time is taken. The first element of the vector “b” corresponds to the first element of vector “a” (and so on).

How do you find the mean time of each track using split function?

Answer: Code is as follows

Question 4 : Treating missing values

Following is the output of the last section :

How do you modify the code, to treat the missing value in the second track record?

Answer: The modified code is as follows :


Data Scientist # 1

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

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

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