Lecture Notes Of Class 5:
Working with Data Frames and Matrices
Objective:
The objective of this class is to
introduce students to data frames and matrices in R programming.
Students will learn how to create, manipulate, and perform operations on data
frames and matrices effectively.
Topics
Covered:
1. Data
Frames in R:
o
What is a Data Frame? A data
frame is a table or a 2D array-like structure in R. It is a collection of
vectors of the same length, where each vector represents a column in the table.
Each column can contain different types of data such as integers, factors, or
strings.
o
Creation of Data Frames: Data
frames can be created using the data.frame() function. The basic syntax for
creating a data frame is:
R
CopyEdit
df <-
data.frame(column1 = c(values), column2 = c(values), column3 = c(values))
Example:
R
CopyEdit
df <-
data.frame(Name = c("John", "Sara", "Tom"),
Age = c(25, 30, 22),
Gender = c("Male", "Female",
"Male"))
print(df)
This will output:
nginx
CopyEdit
Name Age Gender
1 John 25 Male
2 Sara 30
Female
3 Tom 22 Male
o
Subsetting Data Frames: You can
access specific parts of a data frame by using indexing. R provides a variety
of ways to subset data frames:
§ Using the
$ operator to access columns:
R
CopyEdit
df$Name
§ Using
index positions:
R
CopyEdit
df[1, 2] # Accesses the element in the first row and
second column
df[, 1] # Accesses all rows of the first column
df[1, ] # Accesses all columns of the first row
o
Manipulating Data Frames: You can
manipulate data frames in R by adding or removing rows and columns.
§ Add a
column:
R
CopyEdit
df$Salary
<- c(50000, 60000, 45000)
§ Add a
row:
R
CopyEdit
df <-
rbind(df, data.frame(Name = "Emma", Age = 28, Gender = "Female",
Salary = 55000))
§ Remove a
column:
R
CopyEdit
df$Salary
<- NULL
2. Matrices
in R:
o
What is a Matrix? A matrix is a 2D
array-like structure where all elements are of the same type, typically
numbers. Matrices are used to store data in rows and columns, and they are
fundamental for various mathematical computations.
o
Creation of Matrices: A matrix
can be created using the matrix() function. The basic syntax is:
R
CopyEdit
matrix(data,
nrow = rows, ncol = columns, byrow = TRUE/FALSE)
Example:
R
CopyEdit
mat <-
matrix(1:9, nrow = 3, ncol = 3)
print(mat)
This will output:
css
CopyEdit
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
o
Indexing Matrices: Just
like with data frames, matrices can be indexed in several ways:
§ By row
and column index:
R
CopyEdit
mat[1, 2] # Accesses element in the first row, second
column
mat[ , 3] # Accesses all rows of the third column
3. Matrix
Operations:
o
Addition of Matrices: Matrices
of the same dimension can be added together using the + operator:
R
CopyEdit
mat1 <-
matrix(1:9, nrow = 3, ncol = 3)
mat2 <-
matrix(9:1, nrow = 3, ncol = 3)
mat_sum <-
mat1 + mat2
print(mat_sum)
This will output:
css
CopyEdit
[,1] [,2] [,3]
[1,] 10 12 14
[2,] 8 10 12
[3,] 6 8 10
o
Matrix Multiplication: To
perform matrix multiplication, you can use the %*% operator. Note: The
number of columns in the first matrix must be equal to the number of rows in
the second matrix.
R
CopyEdit
mat1 <-
matrix(1:6, nrow = 2, ncol = 3)
mat2 <-
matrix(7:12, nrow = 3, ncol = 2)
mat_mult <-
mat1 %*% mat2
print(mat_mult)
This will output:
css
CopyEdit
[,1] [,2]
[1,] 58 64
[2,] 139 154
Practical
Exercises:
1. Create
and Manipulate Data Frames:
o
Create a data frame containing the following
columns: Student_ID, Name, Marks, Grade.
o
Add 5 rows of data to the data frame with details
of students.
o
Subset the data frame to display only the Name and Marks
columns.
o
Add a new column Age to the data frame.
2. Perform
Matrix Operations:
o
Create two matrices:
§ Matrix A
(3x3) with values from 1 to 9.
§ Matrix B
(3x3) with values from 9 to 1.
o
Perform matrix addition and matrix multiplication
on these matrices and display the results.
Summary:
In this class, we have learned:
- Data
frames are essential for handling and manipulating
tabular data, with various operations such as subsetting, adding/removing
columns, and rows.
- Matrices are
useful for mathematical operations and can be created using the matrix()
function.
- Matrix
operations like addition and multiplication are vital
for mathematical computations and can be easily performed using the
relevant operators in R.
These concepts are fundamental
for data analysis, and mastering them will allow students to handle data
efficiently and perform complex operations.
4o mini
You said:
write answers: Practical
Exercises: Create and Manipulate Data Frames: Create a data frame containing
the following columns: Student_ID, Name, Marks, Grade. Add 5 rows of data to
the data frame with details of students. Subset the data frame to display only
the Name and Marks columns. Add a new column Age to the data frame. Perform
Matrix Operations: Create two matrices: Matrix A (3x3) with values from 1 to 9.
Matrix B (3x3) with values from 9 to 1. Perform matrix addition and matrix
multiplication on these matrices and display the results.
ChatGPT
said:
ChatGPT
Practical
Exercises: Answers
1. Create
and Manipulate Data Frames:
Step 1: Create a data frame
containing the following columns: Student_ID, Name, Marks, Grade.
R
CopyEdit
#
Creating the data frame
students_df
<- data.frame(
Student_ID = c(101, 102, 103, 104, 105),
Name = c("John", "Sara", "Tom",
"Emma", "Jack"),
Marks = c(85, 92, 76, 89, 95),
Grade = c("A", "A+", "B",
"A", "A+")
)
# Display
the data frame
print(students_df)
Output:
css
CopyEdit
Student_ID
Name Marks Grade
1 101
John 85 A
2 102
Sara 92 A+
3 103
Tom 76 B
4 104
Emma 89 A
5 105
Jack 95 A+
Step 2: Subset the data frame to
display only the Name and Marks columns.
R
CopyEdit
#
Subsetting the data frame
subset_df
<- students_df[, c("Name", "Marks")]
# Display
the subset data frame
print(subset_df)
Output:
css
CopyEdit
Name Marks
1 John
85
2 Sara
92
3 Tom
76
4 Emma
89
5 Jack
95
Step 3: Add a new column Age to
the data frame.
R
CopyEdit
# Adding
a new column Age
students_df$Age
<- c(20, 21, 22, 23, 21)
# Display
the updated data frame
print(students_df)
Output:
css
CopyEdit
Student_ID
Name Marks Grade Age
1 101
John 85 A 20
2 102
Sara 92 A+ 21
3 103
Tom 76 B 22
4 104
Emma 89 A 23
5 105
Jack 95 A+ 21
2.
Perform Matrix Operations:
Step 1: Create two matrices:
- Matrix
A (3x3) with values from 1 to 9.
- Matrix
B (3x3) with values from 9 to 1.
R
CopyEdit
#
Creating Matrix A (3x3) with values from 1 to 9
Matrix_A <-
matrix(1:9, nrow = 3, ncol = 3)
#
Creating Matrix B (3x3) with values from 9 to 1
Matrix_B <-
matrix(9:1, nrow = 3, ncol = 3)
# Display
both matrices
print("Matrix
A:")
print(Matrix_A)
print("Matrix
B:")
print(Matrix_B)
Output:
less
CopyEdit
Matrix A:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Matrix B:
[,1] [,2] [,3]
[1,] 9 6 3
[2,] 8 5 2
[3,] 7 4 1
Step 2: Perform matrix addition
on the two matrices and display the result.
R
CopyEdit
# Matrix
addition
Matrix_Addition
<- Matrix_A + Matrix_B
# Display
the result
print("Matrix
Addition Result:")
print(Matrix_Addition)
Output:
less
CopyEdit
Matrix Addition
Result:
[,1] [,2] [,3]
[1,] 10 10 10
[2,] 10 10 10
[3,] 10 10
10
Step 3: Perform matrix
multiplication on the two matrices and display the result.
R
CopyEdit
# Matrix
multiplication
Matrix_Multiplication
<- Matrix_A %*% Matrix_B
# Display
the result
print("Matrix
Multiplication Result:")
print(Matrix_Multiplication)
Output:
less
CopyEdit
Matrix Multiplication
Result:
[,1] [,2] [,3]
[1,] 50 32 14
[2,] 66 41 16
[3,] 78 50 22
Summary
of Results:
- Data
Frame Operations:
- A
data frame was created with columns Student_ID, Name, Marks, and Grade.
- A
subset of the data frame displaying only Name and Marks was extracted.
- A
new column Age was added to the data frame.
- Matrix
Operations:
- Matrix
addition resulted in a matrix where all values are 10.
- Matrix
multiplication produced a 3x3 matrix with the computed values.
These operations are fundamental
to handling data in R and will help you in managing data effectively and
performing mathematical computations.
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 Data Frames and Matrices in R
Here are 10 assignments along
with step-by-step solutions and detailed explanations to help reinforce the
concepts of data frames and matrices.
Assignment
1: Create a Data Frame for Employee Details
Objective: Learn to
create and manipulate a data frame with employee details.
Task:
1. Create a
data frame with the following columns: Employee_ID, Name, Salary, Department.
2. Add 5
employee records with the following data:
o
Employee_ID: 1001, 1002, 1003, 1004, 1005
o
Name: "Alice", "Bob",
"Charlie", "David", "Eva"
o
Salary: 50000, 60000, 45000, 70000, 55000
o
Department: "HR", "IT",
"Finance", "Marketing", "IT"
Solution:
R
CopyEdit
# Step 1:
Create the employee data frame
employee_df
<- data.frame(
Employee_ID = c(1001, 1002, 1003, 1004, 1005),
Name = c("Alice", "Bob", "Charlie",
"David", "Eva"),
Salary = c(50000, 60000, 45000, 70000, 55000),
Department = c("HR", "IT",
"Finance", "Marketing", "IT")
)
# Step 2:
Display the data frame
print(employee_df)
Output:
yaml
CopyEdit
Employee_ID
Name Salary Department
1 1001
Alice 50000 HR
2 1002
Bob 60000 IT
3 1003 Charlie 45000
Finance
4 1004
David 70000 Marketing
5 1005
Eva 55000 IT
Assignment
2: Subset Data Frame to Show Only Names and Salaries
Objective: Practice
subsetting a data frame.
Task:
1. Subset
the employee_df to display only the Name and Salary columns.
Solution:
R
CopyEdit
# Step 1:
Subset the data frame
subset_employee
<- employee_df[, c("Name", "Salary")]
# Step 2:
Display the subset
print(subset_employee)
Output:
markdown
CopyEdit
Name Salary
1 Alice
50000
2 Bob
60000
3
Charlie 45000
4 David
70000
5 Eva
55000
Assignment
3: Add a New Column 'Experience' to the Data Frame
Objective: Learn
how to add a new column to a data frame.
Task:
1. Add a
column Experience with values 3, 4, 2, 5, and 4 to the employee_df.
Solution:
R
CopyEdit
# Step 1:
Add the 'Experience' column
employee_df$Experience
<- c(3, 4, 2, 5, 4)
# Step 2:
Display the updated data frame
print(employee_df)
Output:
yaml
CopyEdit
Employee_ID
Name Salary Department Experience
1 1001
Alice 50000 HR 3
2 1002
Bob 60000 IT 4
3 1003 Charlie 45000
Finance 2
4 1004
David 70000 Marketing 5
5 1005
Eva 55000 IT 4
Assignment
4: Find Employees with Salary Greater Than 55,000
Objective: Use
conditional subsetting to filter data.
Task:
1. Subset
the data frame to find employees whose salary is greater than 55,000.
Solution:
R
CopyEdit
# Step 1:
Filter employees with salary greater than 55,000
high_salary_employees
<- employee_df[employee_df$Salary > 55000, ]
# Step 2:
Display the filtered data
print(high_salary_employees)
Output:
yaml
CopyEdit
Employee_ID
Name Salary Department Experience
2 1002
Bob 60000 IT 4
4 1004
David 70000 Marketing 5
Assignment
5: Create and Display a 3x3 Matrix
Objective: Learn to
create and display a matrix.
Task:
1. Create a 3x3
matrix Matrix_A with values from 1 to 9.
2. Display
the matrix.
Solution:
R
CopyEdit
# Step 1:
Create the matrix
Matrix_A <-
matrix(1:9, nrow = 3, ncol = 3)
# Step 2:
Display the matrix
print(Matrix_A)
Output:
css
CopyEdit
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Assignment
6: Perform Matrix Addition
Objective: Practice
matrix addition.
Task:
1. Create
two matrices:
o
Matrix_A (3x3) with values from 1 to 9.
o
Matrix_B (3x3) with values from 9 to 1.
2. Perform
matrix addition on Matrix_A and Matrix_B.
Solution:
R
CopyEdit
# Step 1:
Create Matrix_A and Matrix_B
Matrix_A <-
matrix(1:9, nrow = 3, ncol = 3)
Matrix_B <-
matrix(9:1, nrow = 3, ncol = 3)
# Step 2:
Perform matrix addition
Matrix_Addition
<- Matrix_A + Matrix_B
# Step 3:
Display the result
print(Matrix_Addition)
Output:
css
CopyEdit
[,1] [,2] [,3]
[1,] 10 10 10
[2,] 10 10 10
[3,] 10 10 10
Assignment
7: Perform Matrix Multiplication
Objective: Learn to
perform matrix multiplication.
Task:
1. Create
two matrices:
o
Matrix_A (3x3) with values from 1 to 9.
o
Matrix_B (3x3) with values from 9 to 1.
2. Perform
matrix multiplication on Matrix_A and Matrix_B.
Solution:
R
CopyEdit
# Step 1:
Create Matrix_A and Matrix_B
Matrix_A <-
matrix(1:9, nrow = 3, ncol = 3)
Matrix_B <-
matrix(9:1, nrow = 3, ncol = 3)
# Step 2:
Perform matrix multiplication
Matrix_Multiplication
<- Matrix_A %*% Matrix_B
# Step 3:
Display the result
print(Matrix_Multiplication)
Output:
css
CopyEdit
[,1] [,2] [,3]
[1,] 50 32 14
[2,] 66 41 16
[3,] 78 50 22
Assignment
8: Create a Data Frame for Product Information
Objective: Practice
creating and manipulating data frames with product details.
Task:
1. Create a
data frame with columns: Product_ID, Product_Name, Price, Category.
2. Add 5
rows with the following data:
o
Product_ID: 101, 102, 103, 104, 105
o
Product_Name: "Laptop",
"Phone", "Tablet", "Monitor", "Keyboard"
o
Price: 50000, 30000, 20000, 15000, 1000
o
Category: "Electronics",
"Electronics", "Electronics", "Accessories",
"Accessories"
Solution:
R
CopyEdit
# Step 1:
Create the product data frame
product_df
<- data.frame(
Product_ID = c(101, 102, 103, 104, 105),
Product_Name = c("Laptop", "Phone",
"Tablet", "Monitor", "Keyboard"),
Price = c(50000, 30000, 20000, 15000, 1000),
Category = c("Electronics", "Electronics",
"Electronics", "Accessories", "Accessories")
)
# Step 2:
Display the product data frame
print(product_df)
Output:
arduino
CopyEdit
Product_ID Product_Name Price Category
1 101
Laptop 50000 Electronics
2 102 Phone
30000 Electronics
3 103
Tablet 20000 Electronics
4 104
Monitor 15000 Accessories
5 105
Keyboard 1000 Accessories
Assignment
9: Find Products in 'Electronics' Category with Price Greater Than 20,000
Objective: Apply
conditional filtering on data frames.
Task:
1. Subset
the product data frame to display products in the "Electronics"
category with price greater than 20,000.
Solution:
R
CopyEdit
# Step 1:
Filter products in 'Electronics' with price greater than 20,000
electronics_products
<- product_df[product_df$Category == "Electronics" &
product_df$Price > 20000, ]
# Step 2:
Display the filtered products
print(electronics_products)
Output:
nginx
CopyEdit
Product_ID Product_Name Price Category
1 101
Laptop 50000 Electronics
2 102 Phone
30000 Electronics
Assignment
10: Calculate the Total Salary of Employees in IT Department
Objective: Learn
how to apply conditional aggregation using data frames.
Task:
1. Calculate
the total salary of employees in the "IT" department.
Solution:
R
CopyEdit
# Step 1:
Filter employees in the 'IT' department
it_employees
<- employee_df[employee_df$Department == "IT", ]
# Step 2:
Calculate the total salary
total_salary
<- sum(it_employees$Salary)
# Step 3:
Display the result
print(paste("Total
salary of IT employees is:", total_salary))
Output:
csharp
CopyEdit
[1] "Total
salary of IT employees is: 115000"
These assignments cover a wide
range of practical exercises, providing step-by-step solutions and detailed
explanations on how to work with data frames and matrices in R. These will help
reinforce the students' understanding and improve their ability to perform data
analysis and matrix operations.
4o mini
You said:
WRITE 50 no of MCQs only FROM
ABOVE CONCEPT with explanation
ChatGPT
said:
ChatGPT
Here are 50 multiple-choice
questions (MCQs) based on the concepts of working with data frames and matrices
in R, along with explanations for the correct answers:
Data
Frames in R
1. Which
function is used to create a data frame in R?
o
a) matrix()
o
b) data.frame()
o
c) list()
o
d) array()
Answer: b) data.frame()
Explanation: The data.frame() function is used to create a data frame in
R.
2. What will
be the output of the following code?
R
CopyEdit
df <-
data.frame(a = 1:3, b = c("X", "Y", "Z"))
df[1, 2]
o
a) "X"
o
b) "Y"
o
c) "Z"
o
d) Error
Answer: a)
"X" Explanation: The code accesses the element in the first
row and second column, which is "X".
3. Which of
the following methods can be used to add a new column to a data frame?
o
a) df$new_column <- c(1, 2, 3)
o
b) add_column(df, new_column = c(1, 2, 3))
o
c) df = cbind(df, new_column = c(1, 2, 3))
o
d) All of the above
Answer: d) All
of the above Explanation: All the methods listed can be used to add a
new column to a data frame.
4. How can
you display the structure of a data frame in R?
o
a) str()
o
b) summary()
o
c) head()
o
d) tail()
Answer: a) str()
Explanation: The str() function is used to display the structure of a
data frame.
5. Which of
the following is the correct syntax to subset a data frame?
o
a) df[1, ]
o
b) df[ , 1]
o
c) df[c(1, 2), ]
o
d) All of the above
Answer: d) All
of the above Explanation: All of these are valid ways to subset a data
frame in R.
6. What will
be the result of the following code?
R
CopyEdit
df <-
data.frame(a = 1:5, b = letters[1:5])
df[1:3, "b"]
o
a) "a" "b" "c"
o
b) "b" "c" "d"
o
c) "a" "b" "c"
o
d) Error
Answer: a)
"a" "b" "c" Explanation: The code selects
the first three rows from the column b, resulting in "a",
"b", "c".
7. What does
the following code do?
R
CopyEdit
df$age <-
c(23, 25, 27, 29, 31)
o
a) Adds a column 'age' to the data frame
o
b) Removes a column 'age' from the data frame
o
c) Replaces the 'age' column
o
d) None of the above
Answer: a) Adds
a column 'age' to the data frame Explanation: The code adds a new column
age to the data frame.
8. What is
the default behavior when subsetting a data frame in R?
o
a) It always returns a vector
o
b) It always returns another data frame
o
c) It returns a matrix
o
d) None of the above
Answer: b) It
always returns another data frame Explanation: By default, when
subsetting a data frame, R returns another data frame.
9. How do
you remove a column from a data frame in R?
o
a) df$column <- NULL
o
b) remove(df$column)
o
c) df$column = NA
o
d) df <- df[-"column"]
Answer: a) df$column
<- NULL Explanation: Assigning NULL to a column removes it from the
data frame.
10.
How do you check for missing values in a data
frame?
o
a) is.null()
o
b) is.na()
o
c) check_na()
o
d) has_na()
Answer: b) is.na()
Explanation: The is.na() function is used to check for missing values in
a data frame.
Matrices
in R
11.
Which function is used to create a matrix in R?
o
a) matrix()
o
b) array()
o
c) matrix_create()
o
d) new_matrix()
Answer: a) matrix()
Explanation: The matrix() function is used to create a matrix in R.
12.
What is the default by-row behavior when creating a
matrix in R?
o
a) Fill the matrix row-wise
o
b) Fill the matrix column-wise
o
c) Create a diagonal matrix
o
d) None of the above
Answer: b) Fill
the matrix column-wise Explanation: By default, R fills a matrix
column-wise.
13.
What will be the result of the following code?
R
CopyEdit
mat <-
matrix(1:6, nrow=2, ncol=3)
mat[1, 2]
o
a) 1
o
b) 3
o
c) 2
o
d) 4
Answer: c) 2 Explanation:
The element in the first row and second column is 2.
14.
Which of the following operations can be performed
on matrices in R?
o
a) Matrix addition
o
b) Matrix multiplication
o
c) Matrix transposition
o
d) All of the above
Answer: d) All
of the above Explanation: Matrix addition, multiplication, and
transposition are all supported in R.
15.
What function is used to transpose a matrix in R?
o
a) transpose()
o
b) t()
o
c) transpose_matrix()
o
d) flip()
Answer: b) t() Explanation:
The t() function is used to transpose a matrix in R.
16.
What does the following code do?
R
CopyEdit
mat1 <-
matrix(1:9, nrow = 3, ncol = 3)
mat2 <-
matrix(9:1, nrow = 3, ncol = 3)
mat1 +
mat2
o
a) Matrix subtraction
o
b) Matrix multiplication
o
c) Matrix addition
o
d) Matrix transposition
Answer: c)
Matrix addition Explanation: The code performs matrix addition by adding
corresponding elements of mat1 and mat2.
17.
Which operator is used for matrix multiplication in
R?
o
a) *
o
b) %*%
o
c) +
o
d) -
Answer: b) %*% Explanation:
The %*% operator is used for matrix multiplication in R.
18.
What is the result of matrix multiplication of two
matrices with incompatible dimensions?
o
a) A warning is generated
o
b) The result is a zero matrix
o
c) An error occurs
o
d) The operation is not executed
Answer: c) An
error occurs Explanation: Matrix multiplication requires the number of
columns in the first matrix to match the number of rows in the second matrix.
19.
Which of the following is a valid way to access an
element in a matrix?
o
a) mat[1, 2]
o
b) mat[1, ]
o
c) mat[, 1]
o
d) All of the above
Answer: d) All
of the above Explanation: These are all valid ways to access different
elements in a matrix.
20.
What will be the output of the following code?
R
CopyEdit
mat <-
matrix(1:6, nrow=2, ncol=3)
mat * 2
o
a) Element-wise multiplication
o
b) Matrix multiplication
o
c) Scalar multiplication
o
d) Error
Answer: a)
Element-wise multiplication Explanation: The code performs element-wise
multiplication of the matrix by the scalar 2.
Data
Frames and Matrices Combined
21.
How can you check the dimensions of a data frame in
R?
o
a) dim()
o
b) nrow()
o
c) ncol()
o
d) length()
Answer: a) dim()
Explanation: The dim() function returns the dimensions (number of rows
and columns) of a data frame.
22.
Which function is used to combine data frames by
rows?
o
a) merge()
o
b) rbind()
o
c) cbind()
o
d) combine()
Answer: b) rbind()
Explanation: The rbind() function is used to combine data frames by
rows.
23.
What does colSums() function do on a data frame?
o
a) Calculates the sum of each column
o
b) Calculates the sum of each row
o
c) Calculates the mean of each column
o
d) Returns the number of columns
Answer: a) Calculates
the sum of each column Explanation: The colSums() function computes the
sum of each column in a data frame.
24.
Which of the following functions can be used to get
the number of rows in a matrix?
o
a) nrow()
o
b) ncol()
o
c) dim()
o
d) length()
Answer: a) nrow()
Explanation: The nrow() function is used to get the number of rows in a
matrix.
25.
Which function can be used to convert a matrix to a
data frame?
o
a) as.data.frame()
o
b) data.frame()
o
c) to_df()
o
d) matrix_to_df()
Answer: a) as.data.frame()
Explanation: The as.data.frame() function is used to convert a matrix
into a data frame.
26.
What does the following code do?
R
CopyEdit
df <-
data.frame(matrix(1:6, nrow=2, ncol=3))
o
a) Creates a data frame with 6 rows and 3 columns
o
b) Creates a data frame with 2 rows and 3 columns
o
c) Creates a matrix with 2 rows and 3 columns
o
d) None of the above
Answer: b)
Creates a data frame with 2 rows and 3 columns Explanation: The matrix()
function creates a 2x3 matrix, which is then converted into a data frame.
27.
Which function is used to calculate the sum of all
elements in a matrix?
o
a) sum()
o
b) matrixSum()
o
c) total()
o
d) add()
Answer: a) sum()
Explanation: The sum() function calculates the sum of all elements in a
matrix.
Advanced
Data Frames and Matrix Concepts
28.
Which function is used to convert a factor column
into a numeric column in R?
o
a) as.numeric()
o
b) as.character()
o
c) factor()
o
d) as.data.frame()
Answer: a) as.numeric()
Explanation: The as.numeric() function is used to convert a factor into
a numeric column.
29.
What will be the result of the following code?
R
CopyEdit
df <-
data.frame(x = 1:5, y = 6:10)
df[,"x"]
o
a) 1 2 3 4 5
o
b) [1] 1 2 3 4 5
o
c) Error
o
d) NULL
Answer: a) 1 2 3
4 5 Explanation: The code selects the entire column x from the data
frame.
30.
Which of the following is the correct way to access
the first row of a data frame in R?
o
a) df[1, ]
o
b) df[ ,1]
o
c) df[1]
o
d) df[1,1]
Answer: a) df[1,
] Explanation: The code accesses the first row of the data frame using df[1,
].
4o mini
You said:
write rest 20
ChatGPT
said:
ChatGPT
Sure! Here are the next 20
questions for your R programming quiz:
Advanced
Data Frames and Matrix Concepts
31.
What does the following code do?
R
CopyEdit
df <-
data.frame(a = c(1, 2, 3), b = c(4, 5, 6))
df[ ,2]
o
a) Accesses the first column
o
b) Accesses the second column
o
c) Accesses the second row
o
d) Creates a new column
Answer: b)
Accesses the second column
Explanation: The code selects the second column of the data frame.
32.
Which of the following functions can be used to
find the unique values in a column of a data frame?
o
a) unique()
o
b) distinct()
o
c) values()
o
d) unique_values()
Answer: a) unique()
Explanation: The unique() function is used to find the unique values in
a column or vector.
33.
What is the default value of the na.rm parameter in
the sum() function in R?
o
a) TRUE
o
b) FALSE
o
c) NA
o
d) NULL
Answer: b) FALSE
Explanation: By default, the sum() function does not remove NA values
unless na.rm = TRUE is specified.
34.
Which function is used to rename the columns of a
data frame?
o
a) rename()
o
b) colnames()
o
c) setnames()
o
d) colnames<-()
Answer: b) colnames()
Explanation: The colnames() function is used to rename the columns of a
data frame in R.
35.
What is the output of the following code?
R
CopyEdit
df <-
data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
df[ ,1]
o
a) A vector with values 1, 2, 3
o
b) A data frame with a single column x
o
c) A matrix with 1 column
o
d) Error
Answer: a) A
vector with values 1, 2, 3
Explanation: The code selects the first column as a vector.
36.
How can you merge two data frames by a common
column?
o
a) merge()
o
b) join()
o
c) combine()
o
d) bind()
Answer: a) merge()
Explanation: The merge() function is used to merge two data frames by a
common column.
37.
Which of the following will extract the last row
from a data frame?
o
a) df[nrow(df), ]
o
b) df[ncol(df), ]
o
c) df[ ,nrow(df)]
o
d) df[ ,ncol(df)]
Answer: a) df[nrow(df),
]
Explanation: The code selects the last row of the data frame using nrow().
38.
How can you convert a numeric vector into a factor
in R?
o
a) factor()
o
b) as.factor()
o
c) convert()
o
d) to.factor()
Answer: b) as.factor()
Explanation: The as.factor() function is used to convert a numeric
vector into a factor.
39.
What does the following code do?
R
CopyEdit
df <- data.frame(x
= 1:3, y = c(NA, 5, 6))
sum(df$y,
na.rm = TRUE)
o
a) Returns 11
o
b) Returns 5
o
c) Returns NA
o
d) Returns an error
Answer: a)
Returns 11
Explanation: The na.rm = TRUE parameter removes NA values and sums the
remaining values (5 + 6).
40.
Which function is used to check the structure of a
data frame?
o
a) summary()
o
b) str()
o
c) inspect()
o
d) check()
Answer: b) str()
Explanation: The str() function is used to check the structure of an R
object, including data frames.
Data
Manipulation with dplyr
41.
Which function is used to select specific columns
from a data frame in dplyr?
o
a) filter()
o
b) select()
o
c) choose()
o
d) column()
Answer: b) select()
Explanation: The select() function is used to select specific columns
from a data frame in dplyr.
42.
Which of the following functions is used to filter
rows based on a condition in dplyr?
o
a) filter()
o
b) subset()
o
c) select()
o
d) drop()
Answer: a) filter()
Explanation: The filter() function is used to filter rows in a data
frame based on a condition.
43.
What does the mutate() function in dplyr do?
o
a) Adds new rows to a data frame
o
b) Adds new columns or modifies existing columns
o
c) Deletes columns from a data frame
o
d) Changes the order of rows
Answer: b) Adds
new columns or modifies existing columns
Explanation: The mutate() function is used to add or modify columns in a
data frame.
44.
Which of the following functions is used to sort
data in dplyr?
o
a) order()
o
b) arrange()
o
c) sort()
o
d) rank()
Answer: b) arrange()
Explanation: The arrange() function is used to sort data by one or more
columns.
45.
Which function can be used to group data by one or
more variables in dplyr?
o
a) group_by()
o
b) split()
o
c) aggregate()
o
d) group()
Answer: a) group_by()
Explanation: The group_by() function is used to group data by one or
more variables for further analysis.
46.
What does the summarize() function in dplyr do?
o
a) Creates a new data frame with aggregated data
o
b) Filters rows based on a condition
o
c) Creates new columns in a data frame
o
d) Sorts a data frame
Answer: a)
Creates a new data frame with aggregated data
Explanation: The summarize() function is used to create summary
statistics for grouped data.
47.
Which of the following functions is used to join
two data frames by common columns in dplyr?
o
a) merge()
o
b) join()
o
c) left_join()
o
d) combine()
Answer: c) left_join()
Explanation: The left_join() function is used to join two data frames by
common columns, keeping all rows from the left data frame.
48.
How do you combine rows from two data frames with
the same columns using dplyr?
o
a) bind_rows()
o
b) combine_rows()
o
c) merge_rows()
o
d) row_bind()
Answer: a) bind_rows()
Explanation: The bind_rows() function is used to combine rows from two
data frames with the same columns.
49.
Which function is used to remove missing values
from a data frame in dplyr?
o
a) drop_na()
o
b) na.omit()
o
c) remove_na()
o
d) filter_na()
Answer: a) drop_na()
Explanation: The drop_na() function is used to remove rows with missing
values in a data frame.
Working
with Date and Time
50.
Which function is used to convert a character to a
date in R?
o
a) as.Date()
o
b) toDate()
o
c) convert_date()
o
d) parseDate()
Answer: a) as.Date()
Explanation: The as.Date() function is used to convert a character
string into a Date object in R.

No comments:
Post a Comment