Progress bar in data.table aggregate action

前端 未结 2 520
遇见更好的自我
遇见更好的自我 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:54

    Following up on @jangorecki's excellent answer, here's a way to use a text progress bar:

    library(data.table)
    dt = data.table(a=1:4, b=c("a","b"))
    grpn = uniqueN(dt$b)
    pb <- txtProgressBar(min = 0, max = grpn, style = 3)
    dt[, {setTxtProgressBar(pb, .GRP); Sys.sleep(0.5); sum(a)}, b]
    close(pb)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题