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

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:

主站蜘蛛池模板: 晋宁县| 双辽市| 武定县| 双鸭山市| 正定县| 和平区| 东阳市| 广德县| 根河市| 新竹市| 昭通市| 宜州市| 新平| 佛教| 建昌县| 郯城县| 思南县| 巨鹿县| 湘阴县| 临沧市| 石屏县| 郁南县| 焦作市| 扶绥县| 广西| 宜春市| 察雅县| 宣化县| 荃湾区| 南陵县| 青海省| 南召县| 郯城县| 东港市| 西贡区| 忻城县| 鄯善县| 梅河口市| 卓尼县| 平度市| 溧阳市|