In: Computer Science
The following R code sorts each vector in a list (lst) and stores the new list in lst1. The code uses a for loop.
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE))
lst1 <- list()
for(i in 1:length(lst))
{
lst1[[i]] <- sort(lst[[i]])
}
lst1
Which of the following options does the same thing as the given R code above ?
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
||
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
||
None of the given options |
||
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
5 points
QUESTION 2
Match the R code on the left (with loop) to its equivalent one (without loop) on the right.
|
|
15 points
QUESTION 3
Match the R code with the value of x
(Here: try to work out the solution with hand and double check by running the code. Don't just run the code and get the answer without understanding the logic)
|
|
20 points
QUESTION 4
The following R code extracts the unique items in each vector in a list (lst) and stores the new list in lst1. The code uses a for loop.
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE))
lst1 <- list()
for(i in 1:length(lst))
{
lst1[[i]] <- unique(lst[[i]])
}
lst1
Which of the following options does the same thing as the given R code above ?
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
||
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
||
None of the given options |
||
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE)) |
5 points
Click Save and Submit to save and submit. Click Save All Answers to save all answers.
1.
answer:
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE))
library(purrr)
lst1 <- map(lst,sort)
lst1
2. ANSWER:
V <- c(1,NA,0,4,-1,2)
y <- c()
for(i in 1:3)
{
y <- c(y,V[i])
}
C.
expalantion:
3.Answer : B
C
A
D
20.
ANswer: D
lst <-
list(c1=sample(1:10,10,replace=TRUE),c2=sample(1:20,10,replace=TRUE),c3=sample(1:10,8,replace=TRUE))
library(purrr)
lst1 <- map(lst,unique)
lst1