Select Page
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

1. What will be the output of the following code ?

```> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> lapply(x, mean)```

a)

```\$a
[1] 2.5
\$b
[1] 1.248845
\$c
[1] 1.9935285
Loop Functions 90
\$d
[1] 5.051388```

b)

```\$a
[1] 2.5
\$b
[1] 0.248845
\$c
[1] 0.9935285
Loop Functions 90
\$d
[1] 5.051388```

c)

```\$a
[1] 3.5
\$b
[1] 0.248845
\$c
[1] 0.9935285
Loop Functions 90
\$d
[1] 5.051388```

d) None of the mentioned

Answer: b [Reason:] You can use lapply() to evaluate a function multiple times each with a different argument.

2. Point out the wrong statement :
a) The sapply() function behaves similarly to lapply()
B) With multiple factors and many levels, creating an interaction can result in many levels that are empty
c) apply() can be thought of as a combination of split() and sapply() for vectors only
d) All of the mentioned

Answer: c [Reason:] tapply() can be thought of as a combination of split() and sapply() for vectors only.

3. What will be the output of the following code ?

```> x <- 1:4
> lapply(x, runif)```

a)

```[[1]]
[1] 0.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059```

b)

```[[1]]
[1] 1.02778712
[[2]]
[1] 2.5273108 0.8803191
[[3]]
[1] 3.37306337 0.04795913 0.13862825
[[4]]
[1] 0.3214921 0.1548316 0.1322282 0.2213059```

c)

```[[1]]
[1] 1.02778712
[[2]]
[1] 0.5273108 0.8803191
[[3]]
[1] 0.37306337 0.04795913 0.13862825
[[4]]
[1] 3.3214921 2.1548316 1.1322282 0.2213059```

d) None of the mentioned

Answer: a [Reason:] In the above question, the first argument of runif() is n, and so the elements of the sequence 1:4 all got passed to the n argument of runif().

4. Which of the following code will print the following result ?

```[[1]]
[1] 2.263808
[[2]]
[1] 1.314165 9.815635
[[3]]
[1] 3.270137 5.069395 6.814425
[[4]]
[1] 0.9916910 1.1890256 0.5043966 9.2925392```

a)

```> x <- 1:4
> lapply(x, runif, min = 0, max = 10)```

b)

```> x <- 1:4
> lapply(x, runif, min = 0, max = 9)```

c)

```> x <- 1:3
> lapply(x, runif, min = 0, max = 10)```

d) None of the mentioned

Answer: a [Reason:] Once the call to lapply() is finished, the function disappears and does not appear in the workspace.

5. Point out the correct statement :
a) split() takes elements of the list and passes them as the first argument of the function you are applying
b) You can use tsplit() to evaluate a function single time each with a same argument
c) Sequence of operations is sometimes referred to as “map-reduce”
d) None of the mentioned

Answer: c [Reason:] The results of applying the function over the subsets are then collated and returned as an object.

6. What will be the output of the following code ?

```> x <- list(a = matrix(1:4, 2, 2), b = matrix(1:6, 3, 2))
> lapply(x, function(elt) { elt[,1] })```

a)

```\$a
[1] 1 2
\$b
[1] 1 2 3```

b)

```\$a
[1] 1 2 3
\$b
[1] 1 2 3```

c)

```\$a
[1] 1 2 3
\$b
[1] 1 2```

d) All of the mentioned

Answer: a [Reason:] You can put an arbitrarily complicated function definition inside lapply().

7. The _____ function takes a vector or other objects and splits it into groups determined by a factor or list of factors.
a) apply()
b) lsplit()
c) split()
d) mapply()

Answer: c [Reason:] The combination of split() and a function like lapply() or sapply() is a common paradigm in R.

8. Which of the following is valid body of split function ?
a) function (x, f)
b) function (x, drop = FALSE, …)
c) function (x, f, drop = FALSE, …)
d) all of the mentioned

Answer: c [Reason:] x is a vector (or list) or data frame.

9.What will be the output of the following code ?

```> f <- function(elt) {
+ elt[, 1]
+ }
> lapply(x, f)```

a)

```\$a
[1] 1 2
\$b
[1] 1 2 3```

b)

```\$a
[1] 1 2 3
\$b
[1] 1 2 3```

c)

```\$a
[1] 1 2 3
\$b
[1] 1 2```

d) All of the mentioned

Answer: a [Reason:] Whether you use an anonymous function or you define a function first depends on your context.

10. What will be the output of following code ?

```> x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
> sapply(x, mean)```

a)

```a b c d
2.500000 -0.251483 1.481246 4.968715```

b)

```a b c d
2.500000 -3.251483 2.481246 5.968715```

c)

```a b c d
3.500000 0.251483 1.481246 4.968715```

d) Error