这是我的数据
df=data.frame("indication"=c(1,1,0,0,1,0,1,1),"indication2"=c(1,0,1,0,1,0,0,1),"grade"=c(65,67,80,20,100,91,70,55),
"group"=c("blue","red","green","blue","green","blue","red","green"))
indication indication2 grade group
1 1 1 65 blue
2 1 0 67 red
3 0 1 80 green
4 0 0 20 blue
5 1 1 100 green
6 0 0 91 blue
7 1 0 70 red
8 1 1 55 green
我试图用pivotTable 做的是添加第三行来计算列GRADE 的平均值。
我的代码:
pt$addData(df)
pt$addColumnDataGroups("group")
pt$addRowGroup(variableName="indication", values="1",caption = "indication")
pt$addRowGroup(variableName="indication2", values="1",caption = "indication2")
pt$defineCalculation(calculationName="Total", summariseExpression="n()")
cg1 <- pt$addRowGroup()
pt$defineCalculation(calculationGroupName="mean_grade", calculationName="meanOfGRADE",
summariseExpression="mean(grade, na.rm=TRUE)")
cg1$addCalculationGroups("meanOfGRADE")
pt$addRowCalculationGroups(atLevel = 1)
pt$renderPivot()
很遗憾: