0% found this document useful (0 votes)
53 views2 pages

09 Soln

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views2 pages

09 Soln

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question

Design a data frame in R for storing about 20 employee details. Create a CSV file named “input.csv” that
defines all the required information about the employee such as id, name, salary, start_date, dept. Import
into R and do the following analysis. a) Find the total number rows & columns
b) Find the maximum salary
c) Retrieve the details of the employee with maximum salary
d) Retrieve all the employees working in the IT Department.
e) Retrieve the employees in the IT Department whose salary is greater than 20000 and write these details
into another file “output.csv”

Solution
Step 1: Create a CSV file named “input.csv” with employee details.
Here’s an example of how you can create a data frame in R with 20 employee details and then save it to a
CSV file:
# Create a data frame with employee details
employee_data <- data.frame(
id = 1:20,
name = c("John", "Alice", "Bob", "Mary", "David", "Sara", "Michael", "Olivia", "Lucas", "Emma", "James
salary = c(45000, 55000, 60000, 70000, 75000, 62000, 80000, 52000, 58000, 67000, 71000, 59000, 68000,
start_date = as.Date(c("2023-01-15", "2022-11-05", "2023-02-20", "2021-08-10", "2022-05-15", "2022-03-
dept = c("IT", "HR", "IT", "Finance", "IT", "Sales", "IT", "HR", "Finance", "IT", "Sales", "HR", "IT",
)

# Save the data frame to a CSV file


write.csv(employee_data, "input.csv", row.names = FALSE)

Step 2: Import the data from “input.csv” and perform the analysis.
# Import data from "input.csv"
employee_data <- read.csv("input.csv")

# a) Find the total number of rows and columns


n_rows <- nrow(employee_data)
n_cols <- ncol(employee_data)
cat("Total number of rows:", n_rows, "\n")
cat("Total number of columns:", n_cols, "\n")

# b) Find the maximum salary


max_salary <- max(employee_data$salary)
cat("Maximum salary:", max_salary, "\n")

# c) Retrieve the details of the employee with the maximum salary


employee_with_max_salary <- employee_data[employee_data$salary == max_salary, ]
cat("Details of employee with maximum salary:\n")
print(employee_with_max_salary)

# d) Retrieve all the employees working in the IT Department


it_department_employees <- employee_data[employee_data$dept == "IT", ]
cat("Employees working in the IT Department:\n")
print(it_department_employees)

1
# e) Retrieve the employees in the IT Department whose salary is greater than 20000
it_department_high_salary <- it_department_employees[it_department_employees$salary > 20000, ]

# Write these details into another file "output.csv"


write.csv(it_department_high_salary, "output.csv", row.names = FALSE)

Output
## Total number of rows: 20
## Total number of columns: 5
## Maximum salary: 80000
## Details of employee with maximum salary:
## id name salary start_date dept
## 7 7 Michael 80000 2022-12-12 IT
## Employees working in the IT Department:
## id name salary start_date dept
## 1 1 John 45000 2023-01-15 IT
## 3 3 Bob 60000 2023-02-20 IT
## 5 5 David 75000 2022-05-15 IT
## 7 7 Michael 80000 2022-12-12 IT
## 10 10 Emma 67000 2023-03-10 IT
## 13 13 Ethan 68000 2022-04-25 IT
## 16 16 Mia 61000 2022-05-11 IT
## 18 18 Charlotte 72000 2023-01-05 IT

You might also like