Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

how to perform calculation by two factors

$
0
0

I would like to calculate the customer's satisfaction level for each employee each day, my data look like this:

df <- data.frame(
rep(seq(
        from=as.Date("2012-1-1",tz="UTC"),
        to=as.Date("2012-1-30",tz="UTC"),by="day"),5),
as.factor(c(rep(1,30),rep(2,30),rep(3,30), rep(4,30),rep(5,30))),
sample(c(1:5), 150, replace = TRUE))
colnames(df)<-c("time", "staff_NO.", "rating")
head(df)

the satisfaction level for each employee is (number of "5" and "4"rating/total number of ratings).

I know how to calculate the satisfaction level for each employee in general:

staffname<-unique(df$staff_NO.)
satisfied<-c()
for(k in 1: length(staffname)){ 
        print(satisfied[k]<-sum(df$staff_NO.==staffname[k]& (df$rating=="4"|df$rating=="5")) 
                /sum(df$staff_NO.==staffname[k]))
}

but I had problem adding time variable into it, meaning I don't know how to calculate the satisfaction level for each employee each day, can anyone help?

##this is what I ve tried, but it's wrong....

day<-unique(df$time)
staffname<-unique(df$staff_NO.)
satisfied<-c()

for (j in 1: length(day)){
        print(day[j])
        for(k in 1: length(staffname)){ 
             print(satisfied[k]<-sum(df$staff_NO.==staffname[k]& (df$rating=="4"|df$rating=="5")) 
                /sum(df$staff_NO.==staffname[k]))

Viewing all articles
Browse latest Browse all 201839

Trending Articles