To analyze which part of your code is taking a long time, use the Rprof() function; it will tell you how much time was spent in each function, both R and user-written: Rprof("output.file") ... code to profile ... summaryRprof("output.file")