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...
Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element...
Consider a numeric vector x (e.g., x=c(2,3,-1,1.2,2.6,0)), write the R code that determine whether each element of x is (Verify that your code work correctly): (a) positive (b) non-positive (c) even or odd (d) an integer
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.
Using R language: Given a vector or list of numbers, call this variable x , write...
Using R language: Given a vector or list of numbers, call this variable x , write down the code that will create a new vector, y , where all the odd number entries are set to 0. In other words, the first, third, fifth, etc entries should all be 0.
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...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each sublists, and then combines both sublists.. True of False
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT