To give you an example of how this would work with the dplyr
package if you want to calculate the means of Handle by groups of Period
AND Queue
:
require(dplyr)
ctrlmeans <- #data.frame to store your results
ttp1 %.% #data.frane to use for analysis
group_by(Period,Queue) %.% #grouping variables (you can add/remove Queue if you like)
filter(Group == "ACTRL") %.% #use only rows where Group == "ACTRL"
summarize(mean.Handle = mean(Handle)) #makes a summary column with means of Handle by group
ttp1 <- inner_join(ttp1,ctrlmeans,by=c("Period","Queue")) #join the ctrlmeans to the ttp1 data frame
ttp1["Diff"] <- with(ttp1, Handle - mean.Handle) #Add column for the differences
#>ttp1
# NUID Period Queue Group Handle mean.Handle Diff
#1 A063828 201415 1 A 1013 437.5 575.5
#2 C833345 201415 1 ACTRL 425 437.5 -12.5
#3 C833346 201415 1 ACTRL 450 437.5 12.5
#4 C833344 201415 2 B 699 562.5 136.5
#5 Z952754 201415 2 ACTRL 444 562.5 -118.5
#6 Z993876 201415 2 ACTRL 681 562.5 118.5
#7 E254607 201416 1 A 532 347.5 184.5
#8 A000904 201416 1 ACTRL 388 347.5 40.5
#9 A024324 201416 1 ACTRL 307 347.5 -40.5
#10 Y950092 201416 2 B 716 405.0 311.0
#11 A047744 201416 2 ACTRL 430 405.0 25.0
#12 A071164 201416 2 ACTRL 380 405.0 -25.0
if you want to calculate only by groups of Period, just remove Queue
from the filter
statement and from the inner_join
statement