简体   繁体   中英

How to divide each value in a pivot table generated in R with grand total?

The pivot table was generated using the below R script. In the script the two columns are basement and style. This indicates for which style(0,1,2) there are basement present in house or not(basement being 0 or 1)

library(pivottabler)
#qpvt(myData, "style", "basement", "n()")
pt <- PivotTable$new() 
pt$addData(myData)
pt$addColumnDataGroups("basement")
pt$addRowDataGroups("style")
pt$defineCalculation(calculationName="TotalHouses",summariseExpression="n()")
pt$renderPivot()
0 1 total
0 14 25 39
1 0 25 25
2 3 41 44
Total 17 91 108

Is there way that all the cells in the pivot table generated above is divided with the grand total, which is 108 in this case

You can use something like this in your code to get the percentages....

pt$defineCalculation(calculationName="Percentage", caption="%", format="%.1f %%", basedOn="TotalHouses", type="function", calculationFunction=getPercentageOfAllCategories)

Can't check because no raw data provided. See here: pivottabler Counting Small Ns and Row Percentages (R)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM