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

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:

主站蜘蛛池模板: 鹤山市| 平乐县| 读书| 汉中市| 缙云县| 夹江县| 丰镇市| 祁阳县| 甘洛县| 阿克苏市| 舟山市| 台山市| 彝良县| 垫江县| 永善县| 三亚市| 昭苏县| 财经| 绥滨县| 阜南县| 方正县| 临沭县| 濮阳市| 栖霞市| 韶山市| 遂平县| 济南市| 繁昌县| 南宁市| 安徽省| 平遥县| 丰都县| 龙游县| 达孜县| 娄底市| 清河县| 万州区| 错那县| 慈溪市| 和平区| 郓城县|