In thepreviously mentionedpower_function() function, we had to use a for loop to loop through all the values of thejune_pricecolumn of the all_prices4data frame.lapplyallows us to define a function (or use an already existing function) over all the elements of a list or vector and it returns a list. Let's redefine power_function() to allow for the computation of different powers onelementsand then uselapplyto loop through each element of a list or vector and take the powerof each ofthese elementson every iteration of the loop.lapply()has the following format:
Now we are returned the fourth power of the june_price column as a vector.
Now we will again work with a combined array, which has the prices of different items in three different months each for 2017 and 2018. Do you remember the structure of it? It looked like this:
Here, the first matrix corresponds to prices for 2017 and the second matrix corresponds to 2018. We will now recreate this array to become a list of matrices in the following way: