Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - Running time by line of code in dplyr

When I want to estimate the running time of a R code, I use the function system.time().

library(dplyr)

system.time({
    Titanic %>%
        as.data.frame() %>%
        mutate(Dataset = 1) %>%
        bind_rows(as.data.frame(Titanic)) %>%
        mutate_all(funs(replace_na(., NA))) %>% 
        filter(Dataset != 1)
})

# utilisateur     système      écoulé 
#        0.02        0.00        0.02

Question: Is there a way to know the running time of each operations, operations between each pipe (the mutate, then the bind_rows, then the filter, etc.) without running each one by one or without writing several system.time() ?

In this example it is not useful, but sometimes I received a long script, with a long running time, and I would like to identify which operations are the lowest.

I made some research but I didn't find something useful.

like image 316
demarsylvain Avatar asked Apr 05 '19 20:04

demarsylvain


People also ask

How do I see execution time in R?

Call the tic() function, then place any R expression or code or function(), then end it with toc() function call. It will print the execution time of the sleep_func().

What does %>% mean in dplyr?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

Is dplyr slower than base R?

table function, the base R function is almost 4 times and the dplyr function is 3 times slower!

How do I run a line in R?

To execute one specific line: we simply click into the line we would like to execute and press the button with the green arrow ("execute current line or selection; or CTRL+ENTER ). RStudio will copy this line into the R console and execute the line (as if we would enter the command and press enter).


1 Answers

You might be interested into the %L>% pipe from my package pipes :

# devtools::install_github("moodymudskipper/pipes")
library(pipes)
Titanic %L>%
  as.data.frame() %L>%
  mutate(Dataset = 1) %L>%
  bind_rows(as.data.frame(Titanic)) %L>%
  mutate_all(list(~replace_na(., NA))) %L>% 
  filter(Dataset != 1)

# as.data.frame(.)   ~  0.03 sec
# mutate(., Dataset = 1)   ~  0 sec
# bind_rows(., as.data.frame(Titanic))   ~  0 sec
# mutate_all(., list(~replace_na(., NA)))   ~  0 sec
# filter(., Dataset != 1)   ~  0.03 sec
# [1] Class    Sex      Age      Survived Freq     Dataset 
# <0 rows> (or 0-length row.names)
like image 188
Moody_Mudskipper Avatar answered Oct 17 '22 07:10

Moody_Mudskipper