0% found this document useful (0 votes)
120 views

R - Assignment

The document contains 4 problems involving R code to: 1) Perform basic arithmetic operations and find values like cube roots 2) Create lists containing different data types and vectors, find the length and class of lists, and check for common elements 3) Create lists of vectors containing integers within ranges and perform indexing, unlisting, and subset operations 4) Create a list with state names as variables and COVID cases as values, access values using $ operator, and find class and length of list, and subset a state
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views

R - Assignment

The document contains 4 problems involving R code to: 1) Perform basic arithmetic operations and find values like cube roots 2) Create lists containing different data types and vectors, find the length and class of lists, and check for common elements 3) Create lists of vectors containing integers within ranges and perform indexing, unlisting, and subset operations 4) Create a list with state names as variables and COVID cases as values, access values using $ operator, and find class and length of list, and subset a state
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Module – 1

Problem 1: Use R to compute the following values.


(a) 27(38-17)
(b) (147) * (39)

(c) √
(d) 54%3
436
12

(e) 54/3
(f) Find cube of 999
(g)

Answers- a. (38-17)*27
= [1] 567

Problem 2: construct 2 lists containing mentioned data types (Numeric, Character,


Complex, Logical, Vector) and do the following…
a. Create another list which has a vector as an input inside it.
b. Find the length and class of the above created list.
Answer - Input : list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common elements of
both the lists are 3 and 4

Input : list1 = [1, 2, 3, 4, 5]


list2 = [6, 7, 8, 9]
Output : No common elements
Explanation: They do not have any
elements in common in between them

Problem 3: Create a list of two vectors containing integers (numbers from 1 to 10 in one and
5 to 15 in other)
 Index the 8th element of the first list and 8th element of second list
 Unlist the second back to atomic vectors
 Subset the new list from 6th element to 14th element
Answer - X <- c(3, 2, 4)
Y<-c(1,2)
Z<-X*Y
## Warning in X * Y: longer object length is not a multiple of shorter object
## length
Z
## [1] 3 4 4

Problem 4: Create a list of 5 states having state name as variable name and number of
covid-19 cases as its values.
A. Access a few values through its variable name use $ to do so.
B. Find the class and length of the list
C. Subset the third state and its value.

Answer- Load package and data

library(dslabs)
data(murders)

# Use the function names to extract the variable names


names(murders)

You might also like