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.NameGenderDepartmentAttendanceInternal MarkGrade
101ArunMaleCSE9285A
102AnuFemaleECE8878B
103RahulMaleCSE7565C
104MeeraFemaleEEE9591A
105VishnuMaleECE8272B
106PriyaFemaleCSE9088A
107AkhilMaleEEE6855C
108NehaFemaleECE8576B
109KiranMaleCSE7869C
110DivyaFemaleEEE9489A
111SandeepMaleECE7261C
112LakshmiFemaleCSE8781A

Tasks

Part A – Creating and Exploring the Data Frame

  1. Create the data frame students.
  2. Display the complete data frame.
  3. Display the first five records.
  4. Display the last five records.
  5. Display the structure of the data frame using str().
  6. Display summary information using summary().
  7. Find the number of rows and columns.
  8. Display the column names.
  9. Access the Attendance column.
  10. Access the Internal Mark column.

Part B – Understanding Factors

Convert the following variables into factors:

  • Gender
  • Department
  • Grade

For example:

students$Gender <- factor(students$Gender)

Then:

  1. Display the structure of the data frame again.
  2. Display the levels of Gender.
  3. Display the levels of Department.
  4. Display the levels of Grade.
  5. Find the number of students in each department.
  6. Find the number of male and female students.
  7. Find the number of students in each grade.

Useful functions:

levels()
table()

Part C – Data Selection and Filtering

Perform the following operations:

  1. Display students whose attendance is greater than 85%.
  2. Display students whose internal mark is greater than 75.
  3. Display students who obtained grade A.
  4. Display students belonging to the CSE department.
  5. Display female students from the ECE department.
  6. Display students whose attendance is below 75%.

For example:

students[students$Attendance > 85, ]

Part D – Sorting

  1. Sort the students according to their internal marks in ascending order.
  2. Sort the students according to their internal marks in descending order.
  3. Identify the student who obtained the highest internal mark.
  4. Identify the student with the lowest attendance.

Part E – Statistical Analysis

Calculate:

  1. Average attendance of all students.
  2. Average internal mark of all students.
  3. Maximum internal mark.
  4. Minimum internal mark.
  5. Average internal mark of male students.
  6. Average internal mark of female students.
  7. Average internal mark for each department.
  8. Average attendance for each department.

Part F – Group-Based Analysis

This part is particularly useful for understanding factors.

Find:

  1. Number of students in each department.
  2. Average internal mark for each department.
  3. Highest internal mark in each department.
  4. Number of students in each grade.
  5. 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:

  1. Display the updated data frame.
  2. Convert Performance into a factor.
  3. 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

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3