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

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:

主站蜘蛛池模板: 临洮县| 富平县| 临夏县| 南华县| 额济纳旗| 黔江区| 正阳县| 喀什市| 普兰县| 庆元县| 朔州市| 澄迈县| 延庆县| 绩溪县| 定结县| 舟山市| 峨眉山市| 阿拉善左旗| 衡阳县| 砀山县| 泌阳县| 道孚县| 彰武县| 平顺县| 台州市| 永年县| 平乡县| 左权县| 延安市| 湄潭县| 巴东县| 兰考县| 冀州市| 正蓝旗| 江安县| 江城| 巴楚县| 蚌埠市| 伊金霍洛旗| 安丘市| 大港区|