官术网_书友最值得收藏!

Functions in R

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, 4)

We can see that the june_price column is taken to the fourth power as follows:

主站蜘蛛池模板: 卢龙县| 咸阳市| 新蔡县| 石柱| 金湖县| 静宁县| 休宁县| 广南县| 安龙县| 自治县| 三明市| 葫芦岛市| 黄山市| 台南市| 新蔡县| 水城县| 德江县| 武夷山市| 九龙城区| 襄城县| 府谷县| 石台县| 克拉玛依市| 东安县| 罗平县| 张家口市| 铁岭县| 昌乐县| 永新县| 遵义县| 湛江市| 孟津县| 团风县| 荥经县| 迭部县| 都安| 朝阳区| 门头沟区| 维西| 大宁县| 丰县|