R語言是統計分析、繪圖和統計編程的強大工具,是一個開源的軟件平台。現在對R感興趣的人越來越多,雖然R語言相對簡單易學,但學習R仍需要一定的編程基礎。本專題將會循序漸進向大家介紹R語言的相關基礎。
R包的介紹
R 包是由社區開發(developed by the community)的功能(functions)和數據集(data sets)的集合。
包是R函數、數據、預編譯代碼以一種定義完善的格式組成的集合,包括R程序,運行該程序的其他語言(例如C語言),解釋這個程序功能、方法的幫助文檔,例子、測試數據等。
R自帶了一系列默認包,包括base、datasets、utils、grDevices、graphics、stats以及methods。這些包提供了很多的默認函數和數據集,我們可以無需下載直接使用。
查看 R 包的安裝目錄
我們可以使用以下函數來查看 R 包的安裝目錄:
> .libPaths() [1] "D:/R/R-4.0.5/library" >
注意:此語言區分大小寫!小寫顯示錯誤。
查看已安裝的包
我們可以使用以下函數來查看已安裝的包:
library()
輸出結果如下:
圖書館‘D:/R/R-4.0.5/library’里有個程輯包: base The R Base Package boot Bootstrap Functions (Originally by Angelo Canty for S) class Functions for Classification cluster "Finding Groups in Data": Cluster Analysis Extended Rousseeuw et al. codetools Code Analysis Tools for R compiler The R Compiler Package datasets The R Datasets Package foreign Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase', ... graphics The R Graphics Package grDevices The R Graphics Devices and Support for Colours and Fonts grid The Grid Graphics Package KernSmooth Functions for Kernel Smoothing Supporting Wand & Jones (1995) lattice Trellis Graphics for R MASS Support Functions and Datasets for Venables and Ripley's MASS Matrix Sparse and Dense Matrix Classes and Methods methods Formal Methods and Classes mgcv Mixed GAM Computation Vehicle with Automatic Smoothness Estimation nlme Linear and Nonlinear Mixed Effects Models nnet Feed-Forward Neural Networks and Multinomial Log-Linear Models parallel Support for Parallel computation in R rpart Recursive Partitioning and Regression Trees spatial Functions for Kriging and Point Pattern Analysis splines Regression Spline Functions and Classes stats The R Stats Package stats4 Statistical Functions using S4 Classes survival Survival Analysis tcltk Tcl/Tk Interface tools Tools for Package Development translations The R Translations Package utils The R Utils Package
R包的安裝
R基礎包無法滿足我們的使用時,我們就需要安裝新包。
安裝新包可以直接使用 install.packages() 函數,格式如下:
install.packages("要安裝的包名")
或者我們可以直接在 CRAN 上下載相關包,直接在本地安裝,我們國內一般建議大家使用國內鏡像。
CRAN 為 The Comprehensive R Archive Network 簡稱。
install.packages("./包名稱.zip")
1)官方網址CRAN 下載R包
R官網:https://www.r-project.org/
點擊 CRAN,進入 CRAN Mirrosrs : https://cran.r-project.org/mirrors.html,找到 China ,會發現國內目前有10個鏡像網站,一般建議根據地域分布就近選擇。
China
https://mirrors.tuna.tsinghua.edu.cn/CRAN/ TUNA Team, Tsinghua University https://mirrors.bfsu.edu.cn/CRAN/ Beijing Foreign Studies University https://mirrors.ustc.edu.cn/CRAN/ University of Science and Technology of China https://mirror-hk.koddos.net/CRAN/ KoDDoS in Hong Kong https://mirrors.e-ducation.cn/CRAN/ Elite Education https://mirror.lzu.edu.cn/CRAN/ Lanzhou University Open Source Society https://mirrors.nju.edu.cn/CRAN/ eScience Center, Nanjing University https://mirrors.tongji.edu.cn/CRAN/ Tongji University https://mirrors.sjtug.sjtu.edu.cn/cran/ Shanghai Jiao Tong University https://mirrors.sustech.edu.cn/CRAN/ Southern University of Science and Technology (SUSTech)
選擇Packages,進入 Contributed Packages,你會發現R包數量驚人,截至目前,CRAN包存儲庫提供了17416個可用包。
這里我們可以看到R包列表主要由兩種排列方式,一種是按發布日期排序,另一種按R包文件首字母排序。
當然如果我們知道包的名稱可以直接搜索更快,如 echarts4r
最后根據使用者的電腦系統不同選擇相應的安裝包。
其中,下載路徑中,有三種版本:r-devel:是指開發版本,r-release :是指發行版本,r-oldrel:是指舊版本。使用者可以根據各自需要進行下載。
2)指令下載R包
# 安裝echarts4r包 install.packages("echarts4r")
通過library(),查看已經安裝包已經成功安裝。
R包的加載
R包只需要下載一次,但每次運行這一R包時,首先要調用加載R包。
在R中可以使用載入指令來加載R包:
library("包名")