Progress bar in data.table aggregate action

前端 未结 2 519
遇见更好的自我
遇见更好的自我 2021-01-03 10:57

ddply has a .progress to get a progress bar while it\'s running, is there an equivalent for data.table in R?

2条回答
  •  礼貌的吻别
    2021-01-03 11:58

    Yes, you can use any progress status you want.

    library(data.table)
    dt = data.table(a=1:4, b=c("a","b"))
    dt[, {cat("group:",b,"\n"); sum(a)}, b]
    #group: a 
    #group: b 
    #   b V1
    #1: a  4
    #2: b  6
    

    If you ask about progress in loading csv file with fread then it will automatically be displayed for bigger datasets. Also as mentioned by Sergey in comment you can use verbose argument to get more information, both in fread and in [.data.table.

    If you want the percentage of groups processed.

    grpn = uniqueN(dt$b)
    dt[, {cat("progress",.GRP/grpn*100,"%\n"); sum(a)}, b]
    #progress 50 % 
    #progress 100 % 
    #   b V1
    #1: a  4
    #2: b  6
    

提交回复
热议问题