November 2018
Beginner to intermediate
354 pages
6h 59m
English
In the previously mentioned power_function() function, we had to use a for loop to loop through all the values of the june_price column of the all_prices4 data frame. lapply allows 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 on elements and then use lapply to loop through each element of a list or vector and take the power of each of these elements on every iteration of the loop. lapply() has the following format:
lapply(data, function, arguments_of_the_function)
power_function2 = function(data, power){ data^power}lapply(all_prices4$june_price, power_function2, 4)
As we ...
Read now
Unlock full access