Suppose you have two datasets:
Dataset1:
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:
rating
is greater than or equal to 4, assign “High”.rating
is between 3 and 4, assign “Medium”.rating
is less than 3, assign “Low”.Step3: Return the merged dataset with the added performance_grade
column.
#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)
}
# 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
Popescu (JCU): Lecture 9