Question

In: Computer Science

The following R code sorts each vector in a list (lst) and stores the new list...

  1. 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))
    library(purrr)
    lst1 <- map(lst,sort)
    lst1

    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_dbl(lst,sort)
    lst1

    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))
    library(purrr)
    lst1 <- map_int(lst,sort)
    lst1

5 points   

QUESTION 2

  1. Match the R code on the left (with loop) to its equivalent one (without loop) on the right.

          -       A.       B.       C.   

    V <- c(1,NA,0,4,-1,2)

    y <- c()
    for(i in 1:3)
    {
    y <- c(y,V[i])
    }

          -       A.       B.       C.   

    V <- c(1,NA,0,4,-1,2)

    y <- c()
    for(i in 1:4)
    {
    y <- c(y,V[i])
    }

          -       A.       B.       C.   

    df <- data.frame(col1 = sample(1:3,3,replace=FALSE),
                     col2 = sample(1:3,3,replace=FALSE),
                     col3 = sample(1:3,3,replace=FALSE))

    y <- c()
    for(i in 1:ncol(df))
    {
    y <- c(y,sum(df[,i]))
    }

    A.

    V <- c(1,NA,0,4,-1,2)

    y <- V[1:3]

    B.

    df <- data.frame(col1 = sample(1:3,3,replace=FALSE),
                     col2 = sample(1:3,3,replace=FALSE),
                     col3 = sample(1:3,3,replace=FALSE))

    y <- colSums(df)

    C.

    V <- c(1,NA,0,4,-1,2)

    y <- V[1:4]

15 points   

QUESTION 3

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

          -       A.       B.       C.       D.   

    V <- c(1,NA,0,4,-1,2)
    for(i in 1:3)
    {
    y <- V[i]  
    }
    x <- y

          -       A.       B.       C.       D.   

    V <- c(1,NA,0,4,-1,2)
    for(i in 1:4)
    {
    y <- 1
    }
    x <- y
    x

          -       A.       B.       C.       D.   

    df <- data.frame(col1 = sample(1:3,3,replace=FALSE),
                     col2 = sample(1:3,3,replace=FALSE),
                     col3 = sample(1:3,3,replace=FALSE))
    for(i in 1:ncol(df))
    {
    y <- sum(df[,i])
    }
    x <- y
    x

          -       A.       B.       C.       D.   

    library(purrr)
    df <- data.frame(col1 = sample(1:4,4,replace=FALSE),
                     col2 = sample(1:4,4,replace=FALSE),
                     col3 = sample(1:4,4,replace=FALSE))
    y <- map_dbl(df,max)
    x <- min(y)
    x

    A.

    6

    B.

    0

    C.

    1

    D.

    4

20 points   

QUESTION 4

  1. 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))
    library(purrr)
    lst1 <- map_int(lst,unique)
    lst1

    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_dbl(lst,unique)
    lst1

    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))
    library(purrr)
    lst1 <- map(lst,unique)
    lst1

5 points   

Click Save and Submit to save and submit. Click Save All Answers to save all answers.

Solutions

Expert Solution

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


Related Solutions

Find the 95% confidence interval of the mean of a vector in r code. The vector...
Find the 95% confidence interval of the mean of a vector in r code. The vector length is 100.
Data Analysis & Visualization Topic R vector and save the r code in a text file...
Data Analysis & Visualization Topic R vector and save the r code in a text file Problem 1. Create two vectors named v and w with the following contents:      v : 21,10,32,2,-3,4,5,6,7,4,-22      w : -18,72,11,-9,10,2,34,-5,18,9,2 A) Print the length of the vectors B) Print all elements of the vectors C) Print elements at indices 3 through 7. D) Print the sum of the elements in each vector. E) Find the mean of each vector. (Use R's mean() function)...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a...
Use R code Create a vector V with 8 elements (7,2,1,0,3,-1,-3,4): Transform that vector into a rectangular matrix A of dimensions 4X2 (4- rows, 2-columns); Create a matrix transpose to the above matrix A. Call that matrix AT; Calculate matrix products: A*AT and AT*A. Present the results. What are the dimensions of those two product matrices; Square matrixes sometimes have an inverse matrix. Try calculating inverse matrices (or matrixes, if you prefer) of above matrices (matrixes) A*AT and AT*A; Extend...
Please provide R-code for the following question: The second argument to `split` can be a list...
Please provide R-code for the following question: The second argument to `split` can be a list of factors. The result is that all interactions (possible combinations) are used for the groups. In the `ToothGrowth` data set, growth (`len`) is measured for two types of supplements (`supp`) and three doses (`dose`). Split this `len` value into 6 groups.
Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 1.) Suppose 30 students are all taking the same Math 115 and English 101 classes at CSUN. You want to know in which class students tend to do better. The data below represents the class averages of the students in both classes....
Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 2) You want to determine if the average height of men in California is greater than the average height of men in Nebraska. You take a random sample of 30 men in California and 30 men in Nebraska. The data below represents...
Given the list values = [], write code that fills the list with each set of...
Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0 1 0...
This is R language Create a vector that contains the message part of each log entry...
This is R language Create a vector that contains the message part of each log entry in logsin uppercase letters. In your solution, you might want to use the function toupper(). Simply print this vector to the console. # Return vector with uppercase version of message elements in log entries # Apply extract_caps function on logs
hat are the values of the elements in the vector named names_1 after the following code...
hat are the values of the elements in the vector named names_1 after the following code is executed? vector<string> names_1 { "Mike", "Ben", "Joel", "Anne" }; vector<string> names_2 { "Judy", "Samantha", "Kelly" }; names_1.insert(names_1.end(), "Mary"); names_1.erase(names_1.begin());         names_1.insert(names_1.begin() + 2, ++names_2.begin(), names_2.end()); names_1.swap(names_2); names_1.erase(++names_1.begin()); names_1.insert(names_1.begin(), ++names_2.begin(), names_2.begin() + 2); a. Joel, Judy, Samantha b. Judy, Mary, Joel, Mary c. Joel, Anne, Judy, Samantha d. Joel, Judy, Kelly 1.5 points    QUESTION 12 What are the values of the key/value pairs in...
*Please answer the following question using R code* 3. A bank wants to get new customers...
*Please answer the following question using R code* 3. A bank wants to get new customers for their credit card. They try two different approaches in their marketing campaign. The first promises a "cash back" reward, and the second promises low interest rates. A sample of 500 people is mailed the first brochure; of these, 125 get the credit card. A separate sample of 500 people is mailed the second brochure; 150 get the credit card. Are the two campaigns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT