R-Solved QB Unit-1
R-Solved QB Unit-1
R-Solved QB Unit-1
2 Marks:
1. What are the two ways of assignment operators in R? Give an example.
Ans: In R, there are two ways of assignment operators: <- and =. For example:
5. Create and store a sequence of values from 5 to −11 that progresses in steps of 0.3
Ans: To create and store a sequence of values in R, you can use the seq() func on. Here's how you can create
a sequence from 5 to -11 in steps of 0.3:
my_sequence <- seq(5, -11, by = -0.3)
Ans: To delete the last element from a vector in R, you can use the indexing with a nega ve value. For example:
Nega ve indexing in vectors is used to exclude elements from the vector. For example:
Ans: There's a typo in your ques on; it should be baz instead of baz for consistency. To find the value of baz + qux,
you can do:
qux <- 3
result <- baz + qux will add 3 to each element of the baz vector.
9. What is the use of cbind and rbind func ons in Matrix? Give an example
Ans: cbind and rbind are func ons in R used to combine vectors or matrices into a matrix.
cbind combines objects by columns, and rbind combines objects by rows. For example:
x <- c(1, 2, 3)
y <- c(4, 5, 6)
combined <- cbind(x, y) will create a 3x2 matrix with x and y as columns.
Ans: o find the dimension of a matrix in R, you can use the dim() func on. For example:
11.Construct a 4 × 2 matrix that is filled row-wise with the values 4.3, 3.1, 8.2, 8.2,
Ans: To construct a 4x2 matrix filled row-wise with the specified values, you can use the matrix() func on in R. Here's
how you can create it:
mat <- matrix(c(4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, 6.5), nrow = 4, ncol = 2, byrow = TRUE)
Ans: The diag() func on in R is used to create a diagonal matrix or extract the diagonal elements from a matrix. For
example:
To extract the diagonal elements from a matrix mat: diagonal_elements <- diag(mat)
third row of B.
Ans: o replace the third column of matrix B with the values from the third row of B, you can use indexing as follows:
Ans: In R, & is a bitwise AND operator, and && is a logical AND operator. Here's an example:
a <- TRUE
b <- FALSE
a & b will return FALSE, and a && b will also return FALSE.
example.
Ans: To count the number of individual characters in a string in R, you can use the nchar() func on. For example:
Ans: The levels() func on in R is used with factors to get the levels (dis nct categories) of a factor variable. For
example:
list in R is a data structure that can hold a collec on of objects (vectors, data frames, etc.) of different types. For
example:
my_list <- list(name = "John", age = 30, scores = c(85, 90, 78))
Ans:
Ans: You can name list contents in R by using the names() func on or directly when crea ng the list. For example:
Using names(): my_list <- list(1, 2, 3); names(my_list) <- c("A", "B", "C")
22.What is the purpose of a ributes and class func ons? Give an example.
Ans:The a ributes() func on is used to a ach metadata to R objects. The class() func on is used to specify the class
of an object. For example:
x <- c(1, 2, 3)
23.What is the difference between ggplot2 and base R graphics create plots? Give
an example.
Ans: ggplot2 is a popular data visualiza on package in R that provides a more flexible and layered approach to
crea ng plots compared to base R graphics. ggplot2 allows you to build complex, customized plots. Base R graphics
are more simplis c and are created using func ons like plot(). Here's a simple example:
library(ggplot2)
4 And 6 marks
1. Explain seq, rep and length func ons on vectors with example.
Ans:
seq: The seq() func on in R is used to create sequences of numbers. It can be used to generate a
sequence from a star ng value to an ending value, with a specified increment. For example:
sequence <- seq(1, 10, by = 2)
2. Repeat the vector c (-1,3, -5,7, -9) twice, with each element repeated 10 mes,
and store the result. Display the result sorted from largest to smallest.
Ans:
original_vector <- c(-1, 3, -5, 7, -9)
repeated_vector <- rep(original_vector, each = 10)
sorted_vector <- sort(repeated_vector, decreasing = TRUE)
3. How do you extract elements from vectors? Explain it using individual and vector
of indexes with example?
Ans:
Individual Indexing: You can extract elements from a vector by specifying the index of the element you
want. For example:
my_vector <- c(10, 20, 30, 40, 50)
element1 <- my_vector[1] # Extracts the first element (10)
element3 <- my_vector[3] # Extracts the third element (30)
Vector of Indexes: You can extract mul ple elements by providing a vector of indices. For example:
indices <- c(2, 4)
selected_elements <- my_vector[indices] # Extracts the 2nd and 4th elements (20, 40)
4. How do you create matrix in R? Explain with Its necessary a ributes? Give an
example.
Ans: To create a matrix in R, you can use the matrix() func on. The necessary a ributes are the data elements, the
number of rows, and the number of columns.
To omit and overwrite elements in a matrix, you can simply assign new values to the elements you want to
modify. For example, to omit the element in the second row and third column and overwrite an element:
mat <- matrix(1:9, nrow = 3)
mat[2, 3] <- NA # Omit the element
mat[1, 1] <- 99 # Overwrite an element
8. Explain any 3 matrix opera ons using R commands with example. (6)
Ans: Matrix Addi on:
A <- matrix(1:6, nrow = 2)
B <- matrix(7:12, nrow = 2)
result <- A + B # Matrix addi on
Matrix Mul plica on:
10.Explain any 3 rela onal and logical operators used in R, with an example.
ANS:
a. Rela onal operators:
==: Checks if two values are equal. Example: x == y returns TRUE if x is equal to y.
!=: Checks if two values are not equal. Example: x != y returns TRUE if x is not equal to y.
all(): The all() func on returns TRUE if all elements in a logical vector are TRUE. For example
logical_vector <- c(TRUE, TRUE, TRUE)
result <- all(logical_vector) # Returns TRUE
which(): The which() func on returns the indices of elements in a logical vector that are TRUE. For
example:
logical_vector <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
indices <- which(logical_vector) # Returns indices 1, 3, 4
12.Explain cat and paste func ons with necessary arguments in R. Give an example.
Ans:
cat: The cat() func on in R is used to concatenate and print values together. It prints the values without any
separators by default.
cat("Hello", "World")
# Output: Hello World
paste: The paste() func on is used to concatenate strings or other objects with a specified separator.
result <- paste("Hello", "World", sep = ", ")
# Result: "Hello, World"
13.Write a note on escape sequences with example.
Ans:
Escape sequences are used in R to represent special characters within strings. They begin with a backslash
\. For example:
Vidyashree B- Lecturer in BCA @Sharada Thalapadi
8
\n: Represents a newline character.
\t: Represents a tab character.
19.How do you add data columns and combine data frames? Explain with example.
Ans:
You can add data columns to a data frame using the $ operator or by using func ons like cbind(). To
combine data frames, you can use func ons like rbind() or merge(). Here's an example:
Vidyashree B- Lecturer in BCA @Sharada Thalapadi
10
# Create two data frames
df1 <- data.frame(Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 22))
21.Explain Is-Dot Object-Checking Func ons and As-Dot Coercion Func ons with an example.
Ans:
Is-Dot Object-Checking Func ons: These func ons check whether an object belongs to a specific
class or type. For example, is.numeric(), is.character(), is.data.frame().
As-Dot Coercion Func ons: These func ons are used to coerce or convert an object to a specific
class or type. For example, as.numeric(), as.character().
x <- "42"
is_numeric <- is.numeric(x) # Check if x is numeric
22.List and explain graphical parameters used in plot func on in R with example (any 4)
Ans:
The plot() func on in R allows you to specify various graphical parameters to customize the appearance of
the plot. Here are four graphical parameters:
main: Sets the tle of the plot.
xlab: Sets the label for the x-axis.
ylab: Sets the label for the y-axis.
x <- 1:10
y <- x^2
plot(x, y, main = "Quadra c Func on", xlab = "X-axis", ylab = "Y-axis", col = "blue")
23.How do you add Points, Lines, and Text to an Exis ng Plot? Explain with example.
Ans:
You can add points, lines, and text to an exis ng plot using func ons like points(), lines(), and text(). Here's
an example of adding points and text to an exis ng sca er plot:
x <- 1:5
y <- c(3, 6, 4, 9, 7)
plot(x, y, main = "Sca er Plot")
# Add points
points(x, y, col = "red", pch = 2)
# Add text
text(x, y, labels = y, pos = 3)
24.How do you set appearance constants and aesthe c mapping with geoms? Explain with example.
Ans: In the context of data visualiza on using ggplot2, appearance constants and aesthe c mappings can be set
using various geom_ func ons. Appearance constants (such as color, size, and shape) define how the data elements
will look, and aesthe c mappings define how variables in your data map to visual elements. Here's an example using
geom_point():
library(ggplot2)
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 6, 4, 9, 7), group = c("A", "B", "A", "B", "A"))