November 2018
Beginner to intermediate
354 pages
6h 59m
English
We can also achieve the previous result by using a function. Let's name this function square:
square = function(data){ for(price in data){ print(price^2) }}
Now call the function as follows:
square(all_prices4$jan_price)
The following output also shows the squared price of jan_price:

Now suppose we want to have the ability to take elements to any power, not just square. We can attain it by making a little tweak to the function:
power_function = function(data, power){ for(price in data){ print(price^power) }}
Now suppose we want to take the power of 4 for the price in June, we can do the following:
power_function(all_prices4$june_price, ...
Read now
Unlock full access