- Hands-On Geospatial Analysis with R and QGIS
- Shammunul Islam
- 231字
- 2021-06-10 18:44:25
tapply
Now, suppose instead of prices for 2018 only, we have prices for these items for 2017, 2016, and 2015 as well. This new data frame is defined as follows:
all_prices = data.frame(items = rep(c("potato", "rice", "oil"), 4),
jan_price = c(10, 20, 30, 10, 18, 25, 9, 17, 24, 9, 19,27),
mar_price = c(11, 22, 33, 13, 25, 32, 12, 21, 33, 15, 27,39),
june_price = c(20, 25, 33, 21, 24, 40, 17, 22, 27, 13, 18,23)
)
all_prices
The output for the preceding lines of code can be seen as follows:

Now suppose we want to take the mean price of different items for very March in all years. We can do this by using tapply(numerical_variable, categorical_variable, function). So, we will need to convert the items column of the all_prices data frame to a categorical variable to take the mean price.
tapply(all_prices$mar_price, factor(all_prices$items), mean)
This gives us a mean March price for oil, potato, and rice in all years, as follows:

Note the use of factor() to convert the items column to a factor variable.
There are other apply functions, but that's it for now, folks. We will introduce new functions as and when it will be necessary as we proceed to new chapters for geospatial analysis.
To install a new package, we need to write install.packages("package_name"), and to use any package, we need to write load.packages("package_name").
- Div+CSS 3.0網頁布局案例精粹
- 腦動力:C語言函數速查效率手冊
- 程序設計語言與編譯
- 21天學通ASP.NET
- Python Algorithmic Trading Cookbook
- 小型電動機實用設計手冊
- 80x86/Pentium微型計算機原理及應用
- 大數據安全與隱私保護
- Moodle Course Design Best Practices
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- Containers in OpenStack
- 零起點學西門子S7-200 PLC
- 基于企業網站的顧客感知服務質量評價理論模型與實證研究
- 筆記本電腦電路分析與故障診斷
- 手把手教你學Photoshop CS3