因此,我拥有有关客户对我们CS团队服务满意度的数据,如下所示:
employee<-c("A","A","A","B","B","B","C","C","C","D","D","D")
incident<-c(1,2,3,1,2,3,1,2,3,1,2,3)##number of incidents
rating<-c(0,2,0,1,0,2,0,0,2,1,2,2)## 0 means there is no rating, 1 means "not satisfied", 2 means "satisfied"
data.frame(employee, incident,rating)
##satisfaction level for each employee= (number of "satisfied"-number of "unsatisfied")/number of all incident (eg. for employee A, the satisfaction level is(1-0)/3). for B, it's (1-1)/3)
我知道如何计算每个员工的绩效并将其绘制出来,但是如果我有1000名员工,谁能帮助我编写一个函数,该函数能够产生所有员工的客户满意度(并且可能拥有所有员工)在一个图中)?
答案 0 :(得分:0)
假设您的data.frame
被命名为df,使用提供的数据并添加一个Employees向量可以做到以下几点:
创建员工向量:
EmployeeList <- as.character(unique(df[, 1]))
然后计算每个员工的满意度:
f <- c()
sum <- c()
for(k in 1:length(EmployeeList)){
for(i in 0:2){
sum[i+1] <- sum(df[which(df[, "employee"] == EmployeeList[k]), "rating"] == i)
}
f[k] <- (sum[1] - sum[2])/length(which(df[, "employee"] == EmployeeList[k]))
}
这给我们留下了每个员工满意度的向量。现在我们可以使用条形图对此进行可视化:
barplot(f, names.arg = EmployeeList, space = 0, ylim = c(min(f)-1, max(f)+1))
这也适用于更大的数据集。