1. Learn
  2. /
  3. Courses
  4. /
  5. Writing Efficient R Code

Exercise

Calculating row sums

The second bottleneck identified was calculating the row sums.

total <- apply(d, 1, sum)

In the previous exercise you switched the underlying object to a matrix. This makes the above apply operation three times faster. But there's one further optimization you can use - switch apply() with rowSums().

Instructions

100 XP
  • Complete the r_sum() function using rowSums().
  • Use the microbenchmark() function to compare the timings of app() and r_sum().