Speed up the loop operation in R

前端 未结 10 2092
说谎
说谎 2020-11-22 00:04

I have a big performance problem in R. I wrote a function that iterates over a data.frame object. It simply adds a new column to a data.frame and a

10条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:10

    The answers here are great. One minor aspect not covered is that the question states "My PC is still working (about 10h now) and I have no idea about the runtime". I always put in the following code into loops when developing to get a feel for how changes seem to affect the speed and also for monitoring how long it will take to complete.

    dayloop2 <- function(temp){
      for (i in 1:nrow(temp)){
        cat(round(i/nrow(temp)*100,2),"%    \r") # prints the percentage complete in realtime.
        # do stuff
      }
      return(blah)
    }
    

    Works with lapply as well.

    dayloop2 <- function(temp){
      temp <- lapply(1:nrow(temp), function(i) {
        cat(round(i/nrow(temp)*100,2),"%    \r")
        #do stuff
      })
      return(temp)
    }
    

    If the function within the loop is quite fast but the number of loops is large then consider just printing every so often as printing to the console itself has an overhead. e.g.

    dayloop2 <- function(temp){
      for (i in 1:nrow(temp)){
        if(i %% 100 == 0) cat(round(i/nrow(temp)*100,2),"%    \r") # prints every 100 times through the loop
        # do stuff
      }
      return(temp)
    }
    

提交回复
热议问题