Assignment-4 Solution
Assignment-4 Solution
Assignment 4 Solution
x <- c(1, 4, 2, 3)
x[2]
a) 1
b) 2
c) 3
d) 4
e) NA
Explanation: The above code is an example of indexing. x[2] accesses the second element of the
vector x, which is 4.
a) 0
b) 1
c) 2
d) 3
e) 4
Explanation: In R, TRUE is treated as 1 and FALSE as 0 in numerical operations. The sum of the
vector c(TRUE, FALSE, TRUE, TRUE) is 1 + 0 + 1 + 1 = 3.
a) 7
b) 8
c) NA
d) 9
e) 4
Explanation: The sum() function calculates the sum of the vector. The argument na.rm = TRUE
tells R to remove NA values before performing the calculation. Thus, the result is 1 + 2 + 4 = 7.
x <- c(1, 2, 3)
y <- c(4, 5)
z <- x + y
a) 5 7 3
b) 5 7 7
c) NA
d) 5 7
Explanation: When vectors of different lengths are operated on, R recycles the shorter vector.
Here, y is recycled, and z becomes 1+4, 2+5, 3+4. A warning is issued about the length
mismatch.
a) vector(c(1, 2, 3))
b) c(1, 2, 3)
c) c[1, 2, 3]
d) vector[1, 2, 3]
Explanation: The c() function in R combines values into a vector. This syntax is used to create a
simple, one-dimensional array of elements. Other syntax is incorrect.
6) Which data structure in R is used to store multiple elements of different data types?
a) Vector
b) Matrix
c) List
d) Data Frame
Explanation: In R, a list is a data structure that can store multiple elements of different data
types (e.g., numeric, character, logical, and data frames).Whereas Vector can store elements of
only one data type, Matrix can store only one data type (usually numeric or character) in a two-
dimensional structure, and Data Frame can store different data types, but it is more structured
and column-oriented.
a) Descriptive statistics
b) Data visualization
c) Structural information
Explanation: The summary() function provides a quick overview of the data in a data frame,
including minimum, maximum, mean, and quartiles for numeric variables, and frequency counts
for factor variables.
a) seq()
b) range()
c) dim()
Explanation: The seq() function is used to generate sequences of numbers with a specified
starting point, ending point, and increment. range() syntax results in highest and lowest values in
a vector. dim() shows the number of rows and columns in the dataframe.
a) summary()
b) str()
c) head()
d) None of the above
Explanation: summary() provides a summary of each column in a data frame, including measures
like mean, median, and counts for categorical variables. str() displays the structure of an object,
showing data types and a preview of the elements..head() displays the first six rows of a data
frame.
10) Which function in R is used to fit a linear regression model?
a) lm()
b) reg()
c) rq()
d) None of the above
Explanation: lm() fits a linear regression model in R, allowing users to analyze the relationship
between dependent and independent variables. reg () is not a standard function in R. We can
perform quantile regression using the rq function.