# Sample data for employee_data
employee_data <- data.frame(
employee_id = c(101, 102, 103, 104, 105),
name = c("Alice", "Bob", "Charlie", "David", "Eve"),
department = c("HR", "Finance", "Marketing", "IT", "Operations")
)Assignment 2: Exercise 2
Question
Suppose you have two datasets:
Dataset1:
Dataset2:
# Sample data for performance_data
performance_data <- data.frame(
employee_id = c(101, 102, 103, 104, 105),
rating = c(4.5, 3.2, 2.9, 4.8, 3.7),
bonus = c(1000, 800, 500, 1200, 900)
)Instructions
Create a function employee_performance_analysis that takes the two datasets as inputs and performs the following tasks:
Step1: Merge employee_data with performance_data based on the employee_id.
Step2: Calculate a new column performance_grade based on the following criteria:
- If
ratingis greater than or equal to 4, assign “High”. - If
ratingis between 3 and 4, assign “Medium”. - If
ratingis less than 3, assign “Low”.
Step3: Return the merged dataset with the added performance_grade column.
The Approach
#Loading the library
library(dplyr)
# Define function employee_performance_analysis
employee_performance_analysis <- function(df1, df2) {
#Step 1: Merge df1 with df2
merged_df <- left_join(df1, df2, by = "employee_id")
#Step 2: Calculate performance_grade based on rating
merged_df$performance_grade <- ifelse(merged_df$rating >= 4, "High",
ifelse(merged_df$rating >= 3, "Medium", "Low"))
#Returning the df
return(merged_df)
}Testing the Function
# Test the function
employee_performance <- employee_performance_analysis(employee_data, performance_data)
employee_performance employee_id name department rating bonus performance_grade
1 101 Alice HR 4.5 1000 High
2 102 Bob Finance 3.2 800 Medium
3 103 Charlie Marketing 2.9 500 Low
4 104 David IT 4.8 1200 High
5 105 Eve Operations 3.7 900 Medium