Programs to Try using DataFrames in R - Assignment 9
1.Problem Statement
Consider the academic details of 12 students in a class. The dataset contains:
- Roll Number
- Student Name
- Gender
- Department
- Attendance Percentage
- Internal Mark
- Grade
Create a data frame using the following data and perform the specified operations.
Student Data
| Roll No. | Name | Gender | Department | Attendance | Internal Mark | Grade |
|---|---|---|---|---|---|---|
| 101 | Arun | Male | CSE | 92 | 85 | A |
| 102 | Anu | Female | ECE | 88 | 78 | B |
| 103 | Rahul | Male | CSE | 75 | 65 | C |
| 104 | Meera | Female | EEE | 95 | 91 | A |
| 105 | Vishnu | Male | ECE | 82 | 72 | B |
| 106 | Priya | Female | CSE | 90 | 88 | A |
| 107 | Akhil | Male | EEE | 68 | 55 | C |
| 108 | Neha | Female | ECE | 85 | 76 | B |
| 109 | Kiran | Male | CSE | 78 | 69 | C |
| 110 | Divya | Female | EEE | 94 | 89 | A |
| 111 | Sandeep | Male | ECE | 72 | 61 | C |
| 112 | Lakshmi | Female | CSE | 87 | 81 | A |
Tasks
Part A – Creating and Exploring the Data Frame
-
Create the data frame
students. - Display the complete data frame.
- Display the first five records.
- Display the last five records.
-
Display the structure of the data frame using
str(). -
Display summary information using
summary(). - Find the number of rows and columns.
- Display the column names.
-
Access the
Attendancecolumn. -
Access the
Internal Markcolumn.
Part B – Understanding Factors
Convert the following variables into factors:
- Gender
- Department
- Grade
For example:
students$Gender <- factor(students$Gender)
Then:
- Display the structure of the data frame again.
-
Display the levels of
Gender. -
Display the levels of
Department. -
Display the levels of
Grade. - Find the number of students in each department.
- Find the number of male and female students.
- Find the number of students in each grade.
Useful functions:
levels() table()
Part C – Data Selection and Filtering
Perform the following operations:
- Display students whose attendance is greater than 85%.
- Display students whose internal mark is greater than 75.
- Display students who obtained grade A.
- Display students belonging to the CSE department.
- Display female students from the ECE department.
- Display students whose attendance is below 75%.
For example:
students[students$Attendance > 85, ]
Part D – Sorting
- Sort the students according to their internal marks in ascending order.
- Sort the students according to their internal marks in descending order.
- Identify the student who obtained the highest internal mark.
- Identify the student with the lowest attendance.
Part E – Statistical Analysis
Calculate:
- Average attendance of all students.
- Average internal mark of all students.
- Maximum internal mark.
- Minimum internal mark.
- Average internal mark of male students.
- Average internal mark of female students.
- Average internal mark for each department.
- Average attendance for each department.
Part F – Group-Based Analysis
This part is particularly useful for understanding factors.
Find:
- Number of students in each department.
- Average internal mark for each department.
- Highest internal mark in each department.
- Number of students in each grade.
- Average internal mark for each grade.
Students can use:
aggregate()
For example:
aggregate(InternalMark ~ Department, data = students, FUN = mean)
Part G – Creating a New Variable
Create a new column called Performance.
Assign:
-
"Excellent"if Internal Mark ≥ 80 -
"Good"if Internal Mark is between 70 and 79 -
"Average"if Internal Mark is below 70
Then:
- Display the updated data frame.
-
Convert
Performanceinto a factor. - Find the number of students in each performance category.
2.Create a data frame containing employee details. Use factors to represent categorical variables such as Department and Performance Rating, and perform various analyses.
# Create the data frame
employees <- data.frame(
EmpID = c(101, 102, 103, 104, 105),
Name = c("John", "Mary", "Alex", "David", "Sara"),
Department = factor(c("HR", "IT", "Finance", "IT", "HR")),
Salary = c(50000, 70000, 60000, 80000, 55000),
Rating = factor(
c("Good", "Excellent", "Average", "Excellent", "Good"),
levels = c("Average", "Good", "Excellent"),
ordered = TRUE
)
)
EmpID Name Department Salary Rating
1 101 John HR 50000 Good
2 102 Mary IT 70000 Excellent
3 103 Alex Finance 60000 Average
4 104 David IT 80000 Excellent
5 105 Sara HR 55000 Good
Find
1 Department-wise Employee Count
2 Average Salary by Department
3 Employees with Excellent Rating
4 Employees with Rating > Good
5 Highest Paid Employee
Comments
Post a Comment