我正在尝试根据另一个变量“Period”获取单个组“Actrl”的电话处理时间“Handle”的平均“ctrlmeans”。然后,我想通过从数据框中每个人的“句柄”中减去该平均值来创建一个新变量“差异”。
这是我所做的:
> ttp1<-read.csv("ttp1.csv")
> dput(head(ttp1,12))
structure(list(NUID = structure(c(4L, 6L, 7L, 8L, 11L, 12L, 9L,
10L, 1L, 2L, 3L, 5L), .Label = c("A000904", "A024324", "A047744",
"A063828", "A071164", "C833344", "C833345", "C833346", "E254607",
"Y950092", "Z952754", "Z993876"), class = "factor"), Period = c(201415L,
201415L, 201415L, 201415L, 201415L, 201415L, 201416L, 201416L,
201416L, 201416L, 201416L, 201416L), Queue = c(1L, 2L, 1L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L), Group = structure(c(2L, 4L,
3L, 3L, 3L, 3L, 1L, 4L, 3L, 3L, 3L, 3L), .Label = c("A", "A ",
"ACTRL", "B"), class = "factor"), Handle = c(1013L, 699L, 425L,
450L, 444L, 681L, 532L, 716L, 388L, 307L, 430L, 380L)), .Names = c("NUID",
"Period", "Queue", "Group", "Handle"), row.names = c(NA, 12L), class = "data.frame")
我的命令:
> ctrlmeans <- with(subset(ttp1, Group=="ACTRL"), tapply(Handle, Period, mean))
> ctrlmeans
201415 201416
500.00 376.25
> Difference <- ttp1$Handle-ctrlmeans[ttp1$Period]
> Difference
<NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
NA NA NA NA NA NA NA NA NA NA NA NA
为什么我会得到 NA?
如果我在 tapply 命令“队列”中包含一个额外的分组变量,我将如何做到这一点?