0

R:如何绘制 rpivotTable 或 dcast 表,其中汇总列与 Excel 报告相同。

检查数据集,尝试不同的方式在 dcast 和 rpivottable 中添加汇总列,但没有得到它。

检查下面的代码,我在汇总列中得到不正确的总值。

样本数据集就像。

Buyer       year_month(order)       

A           2016-01         
B           2016-01         
C           2016-02         
A           2016-04                 
A           2016-01     
A           2017-01         
B           2017-01         
C           2017-02         
A           2017-04 
A           2017-05         
B           2017-05         
C           2017-06         
A           2017-08     
B           2018-01         
C           2018-02         
A           2018-04         
A           2018-03         
B           2018-03         
C           2018-05         
A           2018-06         
B           2018-07         
A           2018-11         
B           2018-11         
A           2019-01         
B           2019-01         
A           2019-01         
A           2019-02         

我使用的代码但得到的数据不正确。

library( data.table )
library( janitor )

#set data to data.table format
data.table::setDT(sub_data7)

sub_data7[, c("year", "month") := data.table::tstrsplit( year_month, "-" ) ][]

l <- lapply( unique(sub_data7$year),
             function(x) {
                          temp <- sub_data7[ year == x, ]
                          ans <- data.table::dcast(sub_data7, Buyer ~ year_month, fill = 0 )
                          ans <- janitor::adorn_totals(ans, where = c("col"), name = paste0( x, "_Total"))
} )

#bind together
ans <- data.table::rbindlist( l, use.names = TRUE, fill = TRUE )

#melt to ling
ans <- data.table::melt( ans, id.vars = "Buyer" )

ans <- ans[, sum(value, na.rm = TRUE), by = .(Buyer, variable)]

#get the order right
colorder = c("Buyer", sort( names(final)[!names(final) == "Buyer"] ) )


datatable(janitor::adorn_totals( final[, ..colorder ], where = "row" ))

但是在总列中得到不正确的值。

4

1 回答 1

0

你需要使用下面的代码。

library( data.table )
library( janitor )

#add new column as countdate, or elese we've got nothing to sum..
sub_data7 <-mutate(sub_data7,Countdate = ifelse(is.na(sub_data7$year_month),0,1))

#set data to data.table format
data.table::setDT(sub_data7)

#get yearly summarise, with totals
l <- lapply( unique(sub_data7$year),
             function(x) {
                          temp <- sub_data7[ year == x, ]
                          ans <- data.table::dcast(temp[, .(sum(Countdate)), by = .(Buyer, year,Month) ],Buyer ~ year+Month, fill=0)
                          ans <- janitor::adorn_totals(ans, where = c("col"), name = paste0( x, "_Total"))
                          } )

.
.
. # code same it is.
.

#reorder, and get totals by column
datatable(janitor::adorn_totals( final[, ..colorder ], where = "row" ),options = list(pageLength = 25))
于 2020-03-17T08:03:38.127 回答