我有一个包含4行(名称,成绩,主题,标记)的数据透视表。具有独特的组合,但标记可以具有不同的值(基于中期标记,最终标记等)。在制作数据透视表时,我会在单独的行中获取记录,例如,等等。我想在最后一级聚合标记,并将输出作为或或。最后一级应该根据以前的级别来处理标记的聚合。有办法做到这一点吗? attaching an example here, where I want to aggregate marks at last level
答案 0 :(得分:1)
您可以使用表格
table(df$Name, df$Grade, df$Subject, df$Marks)
其中df是您的数据框
答案 1 :(得分:1)
The pivottabler package can produce the table illustrated in the question. (I'm the package author).
Sample Data
Name = c("Rahul", "Rahul", "Rahul", "Rahul", "Rahul", "Rahul", "Ram", "Ram", "Ram", "Ram")
Grade = c(10, 10, 11, 11, 11, 11, 10, 10, 11, 11)
Subject = c("English", "Hindi", "English", "Hindi", "Maths", "Science", "English", "Hindi", "English", "Hindi")
Marks = c(90, 91, 72, 65, 90, 95, 90, 80, 83, 81)
Results = data.frame(Name, Grade, Subject, Marks)
Listing each result separately
library(pivottabler)
pt <- PivotTable$new()
pt$addData(Results)
pt$addRowDataGroups("Name")
pt$addRowDataGroups("Grade", addTotal=FALSE)
pt$addRowDataGroups("Subject", addTotal=FALSE)
pt$addRowDataGroups("Marks", addTotal=FALSE)
pt$defineCalculation(calculationName="Totals", summariseExpression="n()")
pt # to output as plain text to console
pt$renderPivot() # to output as HTML table
Showing the mean of results
library(pivottabler)
pt <- PivotTable$new()
pt$addData(Results)
pt$addRowDataGroups("Name")
pt$addRowDataGroups("Grade", addTotal=FALSE)
pt$addRowDataGroups("Subject", addTotal=FALSE)
pt$defineCalculation(calculationName="Mean", summariseExpression="mean(Marks)")
pt$defineCalculation(calculationName="Totals", summariseExpression="n()")
pt # to output as plain text to console
pt$renderPivot() # to output as HTML table