Back-To-School Deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
The lapply() function
7. lapply() and anonymous functions
Other apply() functions
The split-apply-combine pattern
Summary

Instruction

Good job! In the previous exercise, we first defined the morning_sales function and then used that function in lapply() as the second argument. Another way is to actually place the function definition inside lapply(); in that case, we wouldn't need the morning_sales variable that stores the function definition.

Here's how that would look like:

lapply(sales, function(vector){
  return(sum(vector[7:12]))
})

We defined a nameless function on the spot, right within the argument list. That function takes a single argument (named vector here) and returns the sum of the elements between indices 7 and 12. This is known as an anonymous function.

Exercise

Last month, Smartcom sold 5000 bracelets in just one day.

We'll use this as a benchmark for sales performance. Calculate the total number of pieces sold each day as a fraction of 5000, times 100 (this will actually give you a percentage):

\frac{\text{total\ number\ of\ pieces\ sold\ each\ day}}{5000} \cdot 100

Define this function inside lapply()'s argument list.