Lecture Notes Of Class 4:
Working with Vectors and Lists in R Language
Objective:
This class aims to introduce vectors
and lists in R programming. Students will learn how to create and
manipulate vectors and lists, perform vectorized operations, and use various
functions to interact with these data structures.
Introduction
to Vectors
A vector is a basic data
structure in R that contains elements of the same type, such as numbers,
characters, or logical values. In R, vectors are the foundation for handling
data efficiently.
Creating
Vectors
To create a vector in R, the c()
function (combine function) is used. Here's the syntax:
r
CopyEdit
vector_name
<- c(element1, element2, element3, ...)
Example:
r
CopyEdit
# Create
a numeric vector
num_vector
<- c(1, 2, 3, 4, 5)
# Create
a character vector
char_vector
<- c("apple", "banana", "cherry")
Indexing
Vectors
You can access elements of a
vector using indexing. R indices start from 1, not 0. To access a specific
element, use square brackets [].
Example:
r
CopyEdit
# Access
the first element of num_vector
num_vector[1]
# Access
elements from position 2 to 4
num_vector[2:4]
# Access
the last element using length() function
num_vector[length(num_vector)]
Vectorized
Operations
Vectorized operations allow you
to perform operations on the entire vector without explicitly using loops. R
automatically applies the operation element-wise to each item in the vector.
Example:
r
CopyEdit
# Add 2
to every element of the vector
num_vector
+ 2
#
Multiply each element of the vector by 3
num_vector
* 3
These operations are faster and
more efficient than using loops.
Introduction
to Lists
A list is an R data
structure that can hold elements of different types (e.g., numbers, characters,
vectors, or even other lists). Unlike vectors, lists can contain various data
types in a single object.
Creating
Lists
You can create a list using the list()
function. The syntax is:
r
CopyEdit
list_name
<- list(element1, element2, element3, ...)
Example:
r
CopyEdit
# Create
a list with different data types
my_list <-
list(1, "apple", TRUE, c(1, 2, 3), c("a", "b"))
Accessing
Elements in Lists
To access elements in a list, you
use double square brackets [[ ]] for a single element and single square
brackets [ ] for a sublist.
Example:
r
CopyEdit
# Access
the first element of the list
my_list[[1]]
# Access
the 4th element (which is a vector)
my_list[[4]]
# Access
a sublist
my_list[1:2]
Modifying
List Elements
You can modify elements of a list
by referencing them using their index and assignment.
Example:
r
CopyEdit
# Modify
the first element
my_list[[1]]
<- 10
# Add a
new element to the list
my_list[[6]]
<- "new element"
List
Operations and Functions
Length of
a List
To get the number of elements in
a list, you can use the length() function.
Example:
r
CopyEdit
length(my_list)
List
Concatenation
You can combine lists using the c()
function, just like vectors.
Example:
r
CopyEdit
#
Concatenate two lists
list1 <-
list(1, 2, 3)
list2 <-
list("apple", "banana")
combined_list
<- c(list1, list2)
Other
Useful Functions for Lists
- str(list_name):
Displays the structure of the list.
- names(list_name):
Assigns names to the elements of the list.
Example:
r
CopyEdit
# Assign
names to list elements
names(my_list)
<- c("first", "second", "third", "fourth",
"fifth")
Practical
Exercises
Exercise
1: Create and Manipulate Vectors
1. Create a
numeric vector containing the numbers 5, 10, 15, 20, 25.
2. Use
vectorized operations to:
o
Add 5 to each element.
o
Subtract 3 from each element.
o
Multiply each element by 2.
3. Access
and print the first and last elements of the vector.
Exercise
2: Work with Mathematical Operations on Vectors
1. Create
two numeric vectors of the same length.
2. Perform
the following operations:
o
Add the vectors.
o
Subtract the second vector from the first.
o
Multiply the vectors element-wise.
o
Divide the first vector by the second.
Exercise
3: Create and Manipulate Lists
1. Create a
list that contains:
o
A numeric vector: c(10, 20, 30)
o
A character vector: c("apple",
"orange")
o
A logical value: TRUE
2. Access
and modify the elements in the list.
3. Create a sublist
by extracting the first two elements of the list.
Exercise
4: List Operations
1. Create
two lists containing different data types (e.g., integers, characters).
2. Use the length()
function to find the number of elements in each list.
3. Concatenate
the two lists and display the combined list.
Summary
- Vectors are
one-dimensional arrays that store elements of the same type. They support
vectorized operations, making mathematical operations faster.
- Lists are
versatile data structures that can hold elements of different types and
sizes. You can access, modify, and manipulate list elements using indexing
and functions.
By practicing the creation,
manipulation, and operations on vectors and lists, students will build a strong
foundation for handling and analyzing data in R.
4o mini
You said:
answer this ...Practical
Exercises Exercise 1: Create and Manipulate Vectors Create a numeric vector
containing the numbers 5, 10, 15, 20, 25. Use vectorized operations to: Add 5
to each element. Subtract 3 from each element. Multiply each element by 2.
Access and print the first and last elements of the vector. Exercise 2: Work
with Mathematical Operations on Vectors Create two numeric vectors of the same
length. Perform the following operations: Add the vectors. Subtract the second
vector from the first. Multiply the vectors element-wise. Divide the first
vector by the second. Exercise 3: Create and Manipulate Lists Create a list
that contains: A numeric vector: c(10, 20, 30) A character vector:
c("apple", "orange") A logical value: TRUE Access and
modify the elements in the list. Create a sublist by extracting the first two
elements of the list. Exercise 4: List Operations Create two lists containing
different data types (e.g., integers, characters). Use the length() function to
find the number of elements in each list. Concatenate the two lists and display
the combined list.
ChatGPT
said:
ChatGPT
Practical
Exercises Answers
Exercise
1: Create and Manipulate Vectors
1. Create a
numeric vector containing the numbers 5, 10, 15, 20, 25:
r
CopyEdit
num_vector
<- c(5, 10, 15, 20, 25)
2. Use
vectorized operations:
o
Add 5 to each element:
r
CopyEdit
num_vector
+ 5
Output:
r
CopyEdit
[1] 10 15
20 25 30
o
Subtract 3 from each element:
r
CopyEdit
num_vector
- 3
Output:
r
CopyEdit
[1] 2 7 12
17 22
o
Multiply each element by 2:
r
CopyEdit
num_vector
* 2
Output:
r
CopyEdit
[1] 10 20
30 40 50
3. Access
and print the first and last elements of the vector:
r
CopyEdit
first_element
<- num_vector[1]
last_element
<- num_vector[length(num_vector)]
print(paste("First
element:", first_element))
print(paste("Last
element:", last_element))
Output:
csharp
CopyEdit
[1] "First
element: 5"
[1] "Last
element: 25"
Exercise
2: Work with Mathematical Operations on Vectors
1. Create
two numeric vectors of the same length:
r
CopyEdit
vector1 <-
c(5, 10, 15, 20, 25)
vector2 <-
c(1, 2, 3, 4, 5)
2. Perform
the following operations:
o
Add the vectors:
r
CopyEdit
addition <-
vector1 + vector2
print(addition)
Output:
r
CopyEdit
[1] 6 12 18
24 30
o
Subtract the second vector from the first:
r
CopyEdit
subtraction
<- vector1 - vector2
print(subtraction)
Output:
r
CopyEdit
[1] 4 8 12
16 20
o
Multiply the vectors element-wise:
r
CopyEdit
multiplication
<- vector1 * vector2
print(multiplication)
Output:
r
CopyEdit
[1] 5 20 45
80 125
o
Divide the first vector by the second:
r
CopyEdit
division <-
vector1 / vector2
print(division)
Output:
r
CopyEdit
[1] 5.000000
5.000000 5.000000 5.000000 5.000000
Exercise
3: Create and Manipulate Lists
1. Create a
list that contains:
o
A numeric vector: c(10, 20, 30)
o
A character vector: c("apple",
"orange")
o
A logical value: TRUE
r
CopyEdit
my_list <-
list(c(10, 20, 30), c("apple", "orange"), TRUE)
2. Access
and modify the elements in the list:
o
Accessing the first element (numeric vector):
r
CopyEdit
print(my_list[[1]])
Output:
r
CopyEdit
[1] 10 20
30
o
Modifying the second element (character vector):
r
CopyEdit
my_list[[2]]
<- c("banana", "grape")
print(my_list[[2]])
Output:
r
CopyEdit
[1] "banana"
"grape"
3. Create a
sublist by extracting the first two elements of the list:
r
CopyEdit
sublist <-
my_list[1:2]
print(sublist)
Output:
r
CopyEdit
[[1]]
[1] 10 20
30
[[2]]
[1] "banana"
"grape"
Exercise
4: List Operations
1. Create
two lists containing different data types (e.g., integers, characters):
r
CopyEdit
list1 <-
list(1, 2, 3)
list2 <-
list("apple", "banana", "cherry")
2. Use the length()
function to find the number of elements in each list:
r
CopyEdit
length_list1
<- length(list1)
length_list2
<- length(list2)
print(paste("Length
of list1:", length_list1))
print(paste("Length
of list2:", length_list2))
Output:
csharp
CopyEdit
[1] "Length
of list1: 3"
[1] "Length
of list2: 3"
3. Concatenate
the two lists and display the combined list:
r
CopyEdit
combined_list
<- c(list1, list2)
print(combined_list)
Output:
r
CopyEdit
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "apple"
[[5]]
[1] "banana"
[[6]]
[1] "cherry"
These exercises help reinforce
the concepts of vectors and lists in R. Practice with these examples to get
comfortable with indexing, operations, and manipulations of data structures in
R.
4o mini
You said:
WRITE 10 ASSIGNMENTS FROM ABOVE
CONCEPT WITH STEP BY STEP SOLUTIONS with proper explanation in details
ChatGPT
said:
ChatGPT
Assignments
on Working with Vectors and Lists in R Language
Assignment
1: Create and Manipulate a Vector
Task: Create a
vector my_vector that contains the numbers 5, 10, 15, 20, and 25. Perform the
following operations:
- Add
5 to each element.
- Subtract
3 from each element.
- Multiply
each element by 2.
- Access
the first and last elements.
Solution:
r
CopyEdit
# Step 1:
Create a vector
my_vector
<- c(5, 10, 15, 20, 25)
# Step 2:
Add 5 to each element
add_result
<- my_vector + 5
print(add_result) # Output: [1] 10 15 20 25 30
# Step 3:
Subtract 3 from each element
subtract_result
<- my_vector - 3
print(subtract_result) # Output: [1] 2 7 12 17 22
# Step 4:
Multiply each element by 2
multiply_result
<- my_vector * 2
print(multiply_result) # Output: [1] 10 20 30 40 50
# Step 5:
Access first and last elements
first_element
<- my_vector[1]
last_element
<- my_vector[length(my_vector)]
print(paste("First
Element:", first_element)) #
Output: "First Element: 5"
print(paste("Last
Element:", last_element)) # Output:
"Last Element: 25"
Assignment
2: Perform Mathematical Operations on Vectors
Task: Create
two numeric vectors vector_a and vector_b. Perform the following operations:
- Add
the vectors element-wise.
- Subtract
the second vector from the first.
- Multiply
the vectors element-wise.
- Divide
the first vector by the second.
Solution:
r
CopyEdit
# Step 1:
Create two numeric vectors
vector_a <-
c(5, 10, 15, 20, 25)
vector_b <-
c(1, 2, 3, 4, 5)
# Step 2:
Add the vectors element-wise
add_result
<- vector_a + vector_b
print(add_result) # Output: [1] 6 12 18 24 30
# Step 3:
Subtract the second vector from the first
subtract_result
<- vector_a - vector_b
print(subtract_result) # Output: [1] 4 8 12 16 20
# Step 4:
Multiply the vectors element-wise
multiply_result
<- vector_a * vector_b
print(multiply_result) # Output: [1] 5 20 45 80 125
# Step 5:
Divide the first vector by the second
divide_result
<- vector_a / vector_b
print(divide_result) # Output: [1] 5.0 5.0 5.0 5.0 5.0
Assignment
3: Create and Manipulate a List
Task: Create a
list my_list with three elements:
- A
numeric vector: c(10, 20, 30)
- A
character vector: c("apple", "orange")
- A
logical value: TRUE Perform the following operations:
- Access
the first element (numeric vector).
- Modify
the second element (character vector).
- Create
a sublist by extracting the first two elements.
Solution:
r
CopyEdit
# Step 1:
Create a list with different data types
my_list <-
list(c(10, 20, 30), c("apple", "orange"), TRUE)
# Step 2:
Access the first element (numeric vector)
print(my_list[[1]]) # Output: [1] 10 20 30
# Step 3:
Modify the second element (character vector)
my_list[[2]]
<- c("banana", "grape")
print(my_list[[2]]) # Output: [1] "banana"
"grape"
# Step 4:
Create a sublist by extracting the first two elements
sublist <-
my_list[1:2]
print(sublist)
# Output:
[[1]] [1] 10 20 30
# [[2]] [1] "banana"
"grape"
Assignment
4: Concatenate Two Lists
Task: Create two
lists, list1 and list2, each containing different data types. Concatenate them
and find the length of the combined list.
Solution:
r
CopyEdit
# Step 1:
Create two lists
list1 <-
list(1, 2, 3)
list2 <-
list("apple", "banana", "cherry")
# Step 2:
Concatenate the two lists
combined_list
<- c(list1, list2)
print(combined_list)
# Output:
[[1]] [1] 1
# [[2]] [1] 2
# [[3]] [1] 3
# [[4]] [1] "apple"
# [[5]] [1] "banana"
# [[6]] [1] "cherry"
# Step 3:
Find the length of the combined list
length_combined
<- length(combined_list)
print(paste("Length
of combined list:", length_combined))
# Output: Length of combined list: 6
Assignment
5: Create a List of Different Data Types
Task: Create a
list with three elements:
- An
integer vector: c(1, 2, 3)
- A
character vector: c("apple", "banana")
- A
logical value: FALSE Access the second element and modify it.
Solution:
r
CopyEdit
# Step 1:
Create a list with different data types
my_list <-
list(c(1, 2, 3), c("apple", "banana"), FALSE)
# Step 2:
Access the second element (character vector)
print(my_list[[2]]) # Output: [1] "apple"
"banana"
# Step 3:
Modify the second element (change "banana" to "grape")
my_list[[2]][2]
<- "grape"
print(my_list[[2]]) # Output: [1] "apple"
"grape"
Assignment
6: Vector Operations with Mixed Types
Task: Create a
numeric vector x and a character vector y. Try adding these vectors together
and explain the result.
Solution:
r
CopyEdit
# Step 1:
Create a numeric vector and a character vector
x <- c(1,
2, 3)
y <- c("apple",
"banana", "cherry")
# Step 2:
Try adding the vectors together
result <-
x + y
print(result) # Error: non-numeric argument to binary
operator
Explanation: R does
not allow arithmetic operations between numeric and character types. You must
convert data types to perform operations between them.
Assignment
7: Element-wise Operations on Two Numeric Vectors
Task: Create
two numeric vectors, a and b. Perform the following operations:
- Add
them element-wise.
- Subtract
the second vector from the first.
Solution:
r
CopyEdit
# Step 1:
Create two numeric vectors
a <- c(5,
10, 15)
b <- c(2,
4, 6)
# Step 2:
Add them element-wise
add_result
<- a + b
print(add_result) # Output: [1] 7 14 21
# Step 3:
Subtract the second vector from the first
subtract_result
<- a - b
print(subtract_result) # Output: [1] 3 6 9
Assignment
8: Extract Specific Elements from a List
Task: Create a
list containing a numeric vector, a character vector, and a logical value.
Extract the second element and print it.
Solution:
r
CopyEdit
# Step 1:
Create a list
my_list <-
list(c(10, 20, 30), c("apple", "orange"), TRUE)
# Step 2:
Extract the second element (character vector)
second_element
<- my_list[[2]]
print(second_element) # Output: [1] "apple"
"orange"
Assignment
9: Access and Modify List Elements
Task: Create a
list that contains a numeric vector, a string, and a logical value. Modify the
logical value in the list to FALSE.
Solution:
r
CopyEdit
# Step 1:
Create a list with different elements
my_list <-
list(c(10, 20, 30), "apple", TRUE)
# Step 2:
Modify the logical value to FALSE
my_list[[3]]
<- FALSE
print(my_list) # Output: [[1]] [1] 10 20 30 [[2]] "apple" [[3]] FALSE
Assignment
10: Use length() and sum() on Vectors
Task: Create a
vector of numbers and perform the following:
- Find
the length of the vector.
- Find
the sum of the elements in the vector.
Solution:
r
CopyEdit
# Step 1:
Create a vector
num_vector
<- c(5, 10, 15, 20, 25)
# Step 2:
Find the length of the vector
vector_length
<- length(num_vector)
print(paste("Length
of vector:", vector_length)) #
Output: Length of vector: 5
# Step 3:
Find the sum of the vector
vector_sum
<- sum(num_vector)
print(paste("Sum
of vector:", vector_sum)) # Output:
Sum of vector: 75
These assignments cover all the
basic concepts of vectors and lists in R, from creating and manipulating
vectors and lists to performing mathematical operations.
4o mini
You said:
WRITE 50 no of MCQs only FROM
ABOVE CONCEPT with explanation
ChatGPT
said:
ChatGPT
Multiple
Choice Questions (MCQs) on Working with Vectors and Lists in R Language
MCQ 1:
What will be the output of the
following code?
r
CopyEdit
my_vector
<- c(5, 10, 15, 20, 25)
my_vector
+ 5
a) [1] 10 15 20 25 30
b) [1] 5 10 15 20 25
c) [1] 5 10 15 20 25 5
d) [1] 5 10 15 20 30
Answer: a) [1]
10 15 20 25 30
Explanation: The code adds 5 to each element of my_vector.
MCQ 2:
Which of the following is the
correct syntax to create a numeric vector in R?
a) my_vector <- [1, 2, 3, 4]
b) my_vector <- c(1, 2, 3, 4)
c) my_vector <- 1, 2, 3, 4
d) my_vector <- {1, 2, 3, 4}
Answer: b) my_vector
<- c(1, 2, 3, 4)
Explanation: The c() function is used to create a vector in R.
MCQ 3:
What will be the output of the
following code?
r
CopyEdit
vector_a <-
c(1, 2, 3)
vector_b <-
c(4, 5, 6)
vector_a *
vector_b
a) [1] 4 10 18
b) [1] 5 7 9
c) [1] 4 6 9
d) [1] 1 2 3
Answer: a) [1] 4
10 18
Explanation: This performs element-wise multiplication of the two
vectors.
MCQ 4:
Which function is used to get the
length of a list in R?
a) size()
b) len()
c) length()
d) count()
Answer: c) length()
Explanation: The length() function returns the number of elements in a
vector or list.
MCQ 5:
How can you access the second
element of a list in R?
a) my_list[2]
b) my_list[[2]]
c) my_list[2:]
d) my_list(2)
Answer: b) my_list[[2]]
Explanation: Use [[2]] to access the second element in a list.
MCQ 6:
Which operator is used for
element-wise addition of two vectors in R?
a) &
b) +
c) &&
d) -
Answer: b) +
Explanation: The + operator is used for element-wise addition between
two vectors in R.
MCQ 7:
What will be the output of this R
code?
r
CopyEdit
x <- c(1,
2, 3)
y <- c("apple",
"banana", "cherry")
x + y
a) Error
b) c("1apple", "2banana", "3cherry")
c) c(1, 2, 3)
d) c("apple", "banana", "cherry")
Answer: a) Error
Explanation: R cannot perform arithmetic operations between numeric and
character types.
MCQ 8:
Which function is used to combine
two lists into one in R?
a) combine()
b) c()
c) merge()
d) concat()
Answer: b) c()
Explanation: The c() function is used to concatenate or combine two
lists in R.
MCQ 9:
What will be the output of this R
code?
r
CopyEdit
my_list <-
list(1, 2, 3, 4)
length(my_list)
a) 4
b) 5
c) 3
d) 6
Answer: a) 4
Explanation: The length() function returns the number of elements in the
list, which is 4.
MCQ 10:
What is the default indexing
method in R when accessing list elements?
a) 1-based
b) 0-based
c) 2-based
d) 3-based
Answer: a)
1-based
Explanation: R uses 1-based indexing, meaning the first element of a
vector or list is accessed with 1.
MCQ 11:
What will be the output of the
following code?
r
CopyEdit
my_list <-
list(c(10, 20), c("apple", "orange"), TRUE)
my_list[[2]]
a) "apple"
b) c("apple", "orange")
c) "apple orange"
d) TRUE
Answer: b) c("apple",
"orange")
Explanation: The [[2]] extracts the second element of the list, which is
the character vector c("apple", "orange").
MCQ 12:
Which of the following code
snippets will create a numeric vector?
a) numeric_vector <- c("1", "2", "3")
b) numeric_vector <- c(1, 2, 3)
c) numeric_vector <- (1, 2, 3)
d) numeric_vector <- (1:3)
Answer: b) numeric_vector
<- c(1, 2, 3)
Explanation: The c() function is used to create a numeric vector by
including numeric values directly inside it.
MCQ 13:
How can you multiply a vector x
by a scalar 5 in R?
a) x * 5
b) 5 * x
c) x * 5, 5 * x
d) Both a and b
Answer: d) Both
a and b
Explanation: In R, both x * 5 and 5 * x will perform element-wise
multiplication by the scalar value 5.
MCQ 14:
How do you concatenate two
numeric vectors a and b in R?
a) concatenate(a, b)
b) a + b
c) c(a, b)
d) combine(a, b)
Answer: c) c(a,
b)
Explanation: The c() function is used to concatenate two vectors.
MCQ 15:
Which of the following is a
vectorized operation in R?
a) sum(x)
b) x + 1
c) mean(x)
d) All of the above
Answer: d) All
of the above
Explanation: Vectorized operations like sum(), mean(), and arithmetic
operations (x + 1) are applied element-wise to vectors in R.
MCQ 16:
What is the purpose of the [[ ]]
operator in R?
a) Access the entire list
b) Access a single element of a list
c) Assign a value to a vector
d) Perform a mathematical operation
Answer: b)
Access a single element of a list
Explanation: The [[ ]] operator is used to access individual elements
from a list in R.
MCQ 17:
What will the following code
output?
r
CopyEdit
my_list <-
list(1, 2, 3)
my_list[[3]]
<- 4
my_list
a) [1] 1 2 3
b) [1] 1 2 4
c) [1] 4 2 3
d) [1] 1 2 3 4
Answer: b) [1] 1
2 4
Explanation: The third element in the list is modified from 3 to 4.
MCQ 18:
Which of the following functions
is used to check the type of an object in R?
a) type()
b) object_type()
c) typeof()
d) class()
Answer: c) typeof()
Explanation: The typeof() function in R is used to check the type of an
object.
MCQ 19:
What does the following code do?
r
CopyEdit
my_vector
<- c(1, 2, 3, 4)
my_vector
- 2
a) Subtracts 2 from each element
of the vector
b) Subtracts 2 from the entire vector
c) Subtracts 2 from the second element
d) Multiplies 2 to each element
Answer: a)
Subtracts 2 from each element of the vector
Explanation: The subtraction is performed element-wise.
MCQ 20:
What is the output of this
operation in R:
r
CopyEdit
x <- c(10,
20, 30)
y <- c(1,
2, 3)
x / y
a) [1] 10 10 10
b) [1] 10 5 3
c) [1] 1 2 3
d) [1] 10 20 30
Answer: b) [1]
10 10 10
Explanation: The division is done element-wise: 10/1, 20/2, 30/3.
MCQ 21:
Which of the following is NOT a
valid vector operation in R?
a) Addition of two vectors
b) Multiplication of two vectors
c) Division of two vectors
d) Addition of a vector and a list
Answer: d)
Addition of a vector and a list
Explanation: You cannot directly add a vector and a list in R without
proper handling.
MCQ 22:
What will be the output of the
following R code?
r
CopyEdit
vec <-
c(1, 3, 5, 7)
vec[2:3]
a) c(1, 3, 5)
b) c(3, 5)
c) c(1, 5, 7)
d) c(7, 5)
Answer: b) c(3,
5)
Explanation: The slicing 2:3 selects the second and third elements of
the vector.
MCQ 23:
How can you change an element of
a vector in R?
a) vec[2] <- 10
b) vec(2) = 10
c) vec[2] = 10
d) Both a and c
Answer: d) Both
a and c
Explanation: Both vec[2] <- 10 and vec[2] = 10 modify the second
element of the vector.
MCQ 24:
What will be the result of the
following code?
r
CopyEdit
vec <-
c(2, 4, 6)
vec[vec ==
4] <- 10
vec
a) [1] 10 4 6
b) [1] 2 10 6
c) [1] 2 4 10
d) [1] 2 10 10
Answer: b) [1] 2
10 6
Explanation: The code changes the element 4 in the vector to 10.
MCQ 25:
How can you create an empty list
in R?
a) my_list <- list()
b) my_list <- []
c) my_list <- NULL
d) my_list <- {}
Answer: a) my_list
<- list()
Explanation: The list() function creates an empty list in R.
Continuing
for the next 25 questions...
4o mini
You said:
yes
ChatGPT
said:
ChatGPT
Here are the next 25 MCQs on
working with vectors and lists in R language.
MCQ 26:
What does the following code do?
r
CopyEdit
vec <-
c(1, 2, 3, 4)
vec[vec >
2] <- 10
vec
a) [1] 1 2 3 10
b) [1] 10 2 3 4
c) [1] 1 2 10 10
d) [1] 1 2 3 4
Answer: c) [1] 1
2 10 10
Explanation: The elements greater than 2 are replaced by 10.
MCQ 27:
Which of the following is used to
access elements in a matrix in R?
a) [ ]
b) [[ ]]
c) $
d) ()
Answer: a) [ ]
Explanation: Matrices in R are accessed using square brackets [ ].
MCQ 28:
Which function is used to find
the minimum value in a vector in R?
a) minimum()
b) min()
c) minval()
d) find_min()
Answer: b) min()
Explanation: The min() function returns the minimum value from a vector
or dataset.
MCQ 29:
How would you append a new
element to a vector in R?
a) append(vec, 10)
b) vec + 10
c) vec[10] <- 10
d) vec <<- 10
Answer: a) append(vec,
10)
Explanation: The append() function is used to add an element to the end
of a vector.
MCQ 30:
Which of the following methods
can you use to select all elements from a vector in R?
a) vec[1:length(vec)]
b) vec[]
c) vec[all]
d) vec[1: ]
Answer: b) vec[]
Explanation: Using vec[] will select all the elements of the vector.
MCQ 31:
What will the following code
output?
r
CopyEdit
x <- c(10,
20, 30)
y <- c(5,
5, 5)
x - y
a) [1] 5 5 5
b) [1] 15 15 15
c) [1] 5 10 15
d) [1] 10 10 10
Answer: a) [1] 5
5 5
Explanation: This performs element-wise subtraction between the two
vectors.
MCQ 32:
Which function is used to combine
multiple vectors into one vector in R?
a) combine()
b) concatenate()
c) c()
d) join()
Answer: c) c()
Explanation: The c() function is used to combine multiple vectors into
one.
MCQ 33:
What will be the result of the
following R code?
r
CopyEdit
vec <-
c(2, 4, 6)
vec[2] <-
8
vec
a) [1] 2 4 6
b) [1] 8 4 6
c) [1] 2 8 6
d) [1] 2 4 8
Answer: c) [1] 2
8 6
Explanation: The second element is replaced by 8.
MCQ 34:
Which function is used to find
the sum of a vector in R?
a) sum()
b) total()
c) add()
d) aggregate()
Answer: a) sum()
Explanation: The sum() function is used to calculate the sum of a
vector.
MCQ 35:
How do you check if a vector
contains missing values in R?
a) is_missing()
b) is.na()
c) has_missing()
d) na_check()
Answer: b) is.na()
Explanation: The is.na() function is used to check for missing values in
a vector.
MCQ 36:
What is the output of the
following code?
r
CopyEdit
vec <-
c(1, 2, 3)
vec * 3
a) [1] 3 6 9
b) [1] 1 2 3
c) [1] 6 9 12
d) [1] 1 3 6
Answer: a) [1] 3
6 9
Explanation: Each element of the vector is multiplied by 3.
MCQ 37:
Which of the following code will
result in an error in R?
a) vec[2] <- 10
b) vec[1] <- "hello"
c) vec[3] <- 20.5
d) vec[1] <- TRUE
Answer: b) vec[1]
<- "hello"
Explanation: If vec is a numeric vector, you cannot assign a character
like "hello" to it.
MCQ 38:
What is the result of this
operation?
r
CopyEdit
x <- c(2,
4, 6)
x + 3
a) [1] 5 7 9
b) [1] 2 4 6 3
c) [1] 2 4 6 3 3
d) [1] 2 4 6
Answer: a) [1] 5
7 9
Explanation: The scalar value 3 is added to each element of the vector.
MCQ 39:
Which of the following can be
used to convert a list into a vector in R?
a) as.vector()
b) convert.vector()
c) list_to_vector()
d) vectorize()
Answer: a) as.vector()
Explanation: The as.vector() function is used to convert a list into a
vector.
MCQ 40:
What will be the output of the
following code?
r
CopyEdit
my_list <-
list(1, 2, 3, 4)
my_list[2]
a) [1] 2
b) [1] 1 2 3 4
c) 2
d) list(2)
Answer: a) [1] 2
Explanation: The my_list[2] returns a sublist with the second element of
the original list.
MCQ 41:
How do you combine a vector and a
list in R?
a) c(vector, list)
b) combine(vector, list)
c) c(list, vector)
d) Both a and c
Answer: d) Both
a and c
Explanation: You can combine a vector and a list using c().
MCQ 42:
Which function is used to create
a named list in R?
a) named_list()
b) list()
c) setNames()
d) nameList()
Answer: c) setNames()
Explanation: The setNames() function is used to create a named list in
R.
MCQ 43:
What will the following code
output?
r
CopyEdit
vec <-
c(10, 20, 30)
sum(vec >
15)
a) 2
b) 1
c) 3
d) 0
Answer: a) 2
Explanation: The condition vec > 15 gives a logical vector, and sum()
counts how many TRUE values exist, which is 2.
MCQ 44:
What is the result of the
following R code?
r
CopyEdit
x <- c(3,
6, 9)
y <- c(2,
4, 6)
x / y
a) [1] 1.5 1.5 1.5
b) [1] 6 8 9
c) [1] 5 5 5
d) [1] 1.5 1.5 1.5
Answer: a) [1]
1.5 1.5 1.5
Explanation: This is element-wise division of the two vectors.
MCQ 45:
How do you create a sequence of
numbers from 1 to 10 in R?
a) seq(1, 10)
b) 1:10
c) c(1, 10)
d) sequence(1, 10)
Answer: b) 1:10
Explanation: The colon operator 1:10 generates a sequence of numbers
from 1 to 10.
MCQ 46:
What is the function used to find
the length of a vector in R?
a) length()
b) size()
c) count()
d) dimension()
Answer: a) length()
Explanation: The length() function returns the number of elements in a
vector.
MCQ 47:
What does the following R code
do?
r
CopyEdit
vec <-
c(1, 2, 3)
vec[vec ==
2] <- 20
vec
a) [1] 1 20 3
b) [1] 1 2 3
c) [1] 2 2 3
d) [1] 1 2 20
Answer: a) [1] 1
20 3
Explanation: The element 2 in the vector is replaced with 20.
MCQ 48:
Which function in R is used to
concatenate vectors horizontally?
a) concatenate()
b) cbind()
c) rbind()
d) combine()
Answer: b) cbind()
Explanation: cbind() is used to concatenate vectors or matrices
horizontally.
MCQ 49:
What is the output of the
following R code?
r
CopyEdit
vec <-
c(1, 2, 3)
vec[4] <-
4
vec
a) [1] 1 2 3 4
b) [1] 4 3 2 1
c) [1] 1 2 3
d) Error
Answer: a) [1] 1
2 3 4
Explanation: This appends 4 to the vector as the fourth element.
MCQ 50:
What is the purpose of the unlist()
function in R?
a) To convert a list into a vector
b) To convert a vector into a list
c) To combine multiple vectors
d) To check the type of a list
Answer: a) To
convert a list into a vector
Explanation: The unlist() function is used to flatten a list into a
vector.

No comments:
Post a Comment