title: How to efficiently use Rprof in R?
body: <p>I would like to know if it is possible to get a profile from <code>R</code>-Code in a way that is similar to <code>matlab</code>'s Profiler. That is, to get to know which line numbers are the one's that are especially slow.</p>
  
  <p>What I acchieved so far is somehow not satisfactory. I used <code>Rprof</code> to make me a profile file. Using <code>summaryRprof</code> I get something like the following:</p>
  
  <blockquote>
  <pre><code>$by.self
                    self.time self.pct total.time total.pct
  [.data.frame               0.72     10.1       1.84      25.8
  inherits                   0.50      7.0       1.10      15.4
  data.frame                 0.48      6.7       4.86      68.3
  unique.default             0.44      6.2       0.48       6.7
  deparse                    0.36      5.1       1.18      16.6
  rbind                      0.30      4.2       2.22      31.2
  match                      0.28      3.9       1.38      19.4
  [&lt;-.factor                 0.28      3.9       0.56       7.9
  levels                     0.26      3.7       0.34       4.8
  NextMethod                 0.22      3.1       0.82      11.5
  ...
  </code></pre>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
  <pre><code>$by.total
                        total.time total.pct self.time self.pct
  data.frame                  4.86      68.3      0.48      6.7
  rbind                       2.22      31.2      0.30      4.2
  do.call                     2.22      31.2      0.00      0.0
  [                           1.98      27.8      0.16      2.2
  [.data.frame                1.84      25.8      0.72     10.1
  match                       1.38      19.4      0.28      3.9
  %in%                        1.26      17.7      0.14      2.0
  is.factor                   1.20      16.9      0.10      1.4
  deparse                     1.18      16.6      0.36      5.1
  ...
  </code></pre>
  </blockquote>
  
  <p>To be honest, from this output I don't get where my bottlenecks are because (a) I use <code>data.frame</code> pretty often and (b) I never use e.g., <code>deparse</code>. Furthermore, what is <code>[</code>?</p>
  
  <p>So I tried Hadley Wickham's <code>profr</code>, but it was not any more useful considering the following graph:
  <img src="https://i.stack.imgur.com/YIm0J.png" alt="alt text"></p>
  
  <p><strong>Is there a more convenient way to see which line numbers and particular function calls are slow?<br>
  Or, is there some literature that I should consult?</strong></p>
  
  <p>Any hints appreciated.</p>
  
  <p><strong>EDIT 1:</strong><br>
  Based on Hadley's comment I will paste the code of my script below and the base graph version of the plot. But note, that my question is not related to this specific script. It is just a random script that I recently wrote. <strong>I am looking for a general way of how to find bottlenecks and speed up <code>R</code>-code.</strong></p>
  
  <p>The data (<code>x</code>) looks like this:</p>
  
  <blockquote>
  <pre><code>type      word    response    N   Classification  classN
  Abstract  ANGER   bitter      1   3a              3a
  Abstract  ANGER   control     1   1a              1a
  Abstract  ANGER   father      1   3a              3a
  Abstract  ANGER   flushed     1   3a              3a
  Abstract  ANGER   fury        1   1c              1c
  Abstract  ANGER   hat         1   3a              3a
  Abstract  ANGER   help        1   3a              3a
  Abstract  ANGER   mad         13  3a              3a
  Abstract  ANGER   management  2   1a              1a
  ... until row 1700
  </code></pre>
  </blockquote>
  
  <p>The script (with short explanations) is this:</p>
  
  <blockquote>
  <pre><code>Rprof("profile1.out")
  
  # A new dataset is produced with each line of x contained x$N times 
  y &lt;- vector('list',length(x[,1]))
  for (i in 1:length(x[,1])) {
    y[[i]] &lt;- data.frame(rep(x[i,1],x[i,"N"]),rep(x[i,2],x[i,"N"]),rep(x[i,3],x[i,"N"]),rep(x[i,4],x[i,"N"]),rep(x[i,5],x[i,"N"]),rep(x[i,6],x[i,"N"]))
  }
  all &lt;- do.call('rbind',y)
  colnames(all) &lt;- colnames(x)
  
  # create a dataframe out of a word x class table
  table_all &lt;- table(all$word,all$classN)
  dataf.all &lt;- as.data.frame(table_all[,1:length(table_all[1,])])
  dataf.all$words &lt;- as.factor(rownames(dataf.all))
  dataf.all$type &lt;- "no"
  # get type of the word.
  words &lt;- levels(dataf.all$words)
  for (i in 1:length(words)) {
    dataf.all$type[i] &lt;- as.character(all[pmatch(words[i],all$word),"type"])
  }
  dataf.all$type &lt;- as.factor(dataf.all$type)
  dataf.all$typeN &lt;- as.numeric(dataf.all$type)
  
  # aggregate response categories
  dataf.all$c1 &lt;- apply(dataf.all[,c("1a","1b","1c","1d","1e","1f")],1,sum)
  dataf.all$c2 &lt;- apply(dataf.all[,c("2a","2b","2c")],1,sum)
  dataf.all$c3 &lt;- apply(dataf.all[,c("3a","3b")],1,sum)
  
  Rprof(NULL)
  
  library(profr)
  ggplot.profr(parse_rprof("profile1.out"))
  </code></pre>
  </blockquote>
  
  <p>Final data looks like this:</p>
  
  <blockquote>
  <pre><code>1a    1b  1c  1d  1e  1f  2a  2b  2c  3a  3b  pa  words   type    typeN   c1  c2  c3  pa
  3 0   8   0   0   0   0   0   0   24  0   0   ANGER   Abstract    1   11  0   24  0
  6 0   4   0   1   0   0   11  0   13  0   0   ANXIETY Abstract    1   11  11  13  0
  2 11  1   0   0   0   0   4   0   17  0   0   ATTITUDE    Abstract    1   14  4   17  0
  9 18  0   0   0   0   0   0   0   0   8   0   BARREL  Concrete    2   27  0   8   0
  0 1   18  0   0   0   0   4   0   12  0   0   BELIEF  Abstract    1   19  4   12  0
  </code></pre>
  </blockquote>
  
  <p>The base graph plot:
  <img src="https://i.stack.imgur.com/FtbVh.png" alt="alt text"></p>
  
  <p><a href="https://imgur.com/8grk7.png" rel="noreferrer">Running the script today also changed the ggplot2 graph a little (basically only the labels), see here.</a></p>