Instruction: Create a list in R containing a numeric vector, a character vector, and a matrix. Write a script that extracts and prints the matrix from the list.
Context: This question tests the candidate's understanding of lists in R, which are important data structures for organizing and managing diverse datasets. It also examines the candidate’s ability to manipulate and access data stored in complex structures.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
To start, let's construct the list as per the instruction. We'll call our list myList. The numeric vector will contain integers, the character vector will have a few sample strings, and the matrix will be a simple 2x2 structure with numbers. Here's how we can do it:
```R Creating the components of the list numericVector <- c(1, 2, 3, 4) characterVector <- c("apple", "banana", "cherry") matrixData <- matrix(c(1,2,3,4), nrow=2, byrow=TRUE)...