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

Assignment-4 Solution

The document provides solutions to an assignment on R programming, focusing on code outputs and functions. It includes questions about indexing, summing logical values, handling NA values, and creating vectors, among others. Each question is followed by an explanation of the correct answer and the relevant R concepts.

Uploaded by

ANJALI PATEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment-4 Solution

The document provides solutions to an assignment on R programming, focusing on code outputs and functions. It includes questions about indexing, summing logical values, handling NA values, and creating vectors, among others. Each question is followed by an explanation of the correct answer and the relevant R concepts.

Uploaded by

ANJALI PATEL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Artificial Intelligence for Investments

Assignment 4 Solution

1) What is the output of the following code?

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.

2) What will be the output of the following code?

x <- c(TRUE, FALSE, TRUE, TRUE)


sum(x)

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.

3) What is the output of the following code?

sum(c(1, 2, NA, 4), na.rm = TRUE)

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.

4) What is the output of the following code?

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.

5) What is the correct syntax to create a vector in R?

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.

7) What does the summary() function provide for a data frame in R?

a) Descriptive statistics

b) Data visualization

c) Structural information

d) Code execution time

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.

8) Which function is used to generate a sequence of numbers in R?

a) seq()

b) range()

c) dim()

d) None of the above

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.

9) Which function is used to check the structure of a data frame in R?

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.

You might also like