R中包的操作.doc_第1页
R中包的操作.doc_第2页
R中包的操作.doc_第3页
R中包的操作.doc_第4页
R中包的操作.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

R语言中包的操作1. 列出包所在库的路径.libPaths()1 C:/Program Files/R/R-3.0.2/library列出一个特定包的安装路径:a require(foreign)Loading required package: foreignis.logical(require(foreign)1 TRUE4. 包的更新update.packages()5. 包的帮助信息 格式如下,可以查看包中的函数以及说明help(package=ggplot2)6. 查看本地的包6.1 查看默认加载的包,忽略基本的包getOption(defaultPackages) getOption(defaultPackages)1 datasets utils grDevices graphics stats methods7 ggplot26.2 查看当前已经加载过的包(.packages()1 ggplot2 stats graphics grDevices utils datasets methods base6.3 要显示所有可用的包(.packages(all.available=TRUE) (.packages(all.available=TRUE)1 abind agricolae aplpack base bitops6 boot car caTools class cluster11 codetools colorRamps colorspace compiler datasets16 Defaults devtools dichromat digest doBy21 e1071 effects ellipse evaluate foreign26 formatR Formula gdata ggplot2 ggthemes31 gmodels gplots graphics grDevices grid36 gtable gtools highr Hmisc httr41 KernSmooth knitr labeling lattice latticeExtra46 leaps lme4 lmtest LSD manipulate51 markdown MASS Matrix matrixcalc memoise56 methods mgcv minqa multcomp munsell61 mvtnorm nlme nnet nortest parallel66 pixmap plyr proto psych quantmod71 Rcmdr RColorBrewer Rcpp RcppEigen RCurl76 relimp reshape2 rgl rJava RODBC81 rpart rstudio samplesize sandwich scales86 schoolmath sciplot sem spatial splines91 stats stats4 stringr survival tcltk96 tcltk2 TH.data tools TTR utils101 VennDiagram whisker XLConnect xts zoo7. 卸载包detach(),这是library()的反向操作,此操作主要是为了避免某些包中的函数名称相同,造成冲突,注意与library()的参数不同,detach()参数为detach(package:包的名称),library(包的名称)。例如 library(ggplot2) #加载包 (.packages() #列出当前已经加载的包1 ggplot2 stats graphics grDevices utils datasets7 methods base detach(package:ggplot2) # 卸载ggplot2包 (.packages() #列出当前已经加载的包1 stats graphics grDevices utils datasets methods7 base8自定义启动时候的加载包如果需要长期使用某个包的话,每次开启都需要输入library(),比较麻烦,因此可以让R启动时自动加载某些包。在R的安装目录/etc/Rprofile.site加入下载语句:例如让R启动时自动加载ggplot2包local(old = 2.9.0), graphics, stats NA NA car R (= 2.1.1), stats, graphics, MASS, nnet, survival NA NA class R (= 2.5.0), stats, utils MASS NA cluster R (= 2.9.0), stats, graphics, utils NA NA Suggests Enhances OS_type License Built base NA NA NA Part of R 2.11.1 2.11.1boot survival NA NA Unlimited 2.11.1car alr3, leaps, lmtest, sandwich, mgcv, rgl NA NA GPL (= 2) 2.11.1class NA NA NA GPL-2 | GPL-3 2.11.1cluster NA NA NA GPL (= 2) 2.11.1From this output, we will first focus on thePackageandPrioritycolumns. ThePackagecolumn gives the name of the package and thePrioritycolumn indicates what is needed to use functions from the package. IfPriorityis base, then the package is already installed and loaded, so all of its functions are available upon opening R. IfPriorityis recommended, then the package was installed with base R, but not loaded. Before using the commands from this package, the user will have to load it with thelibrarycommand,e.g.library(boot). IfPriorityis NA, then the package was installed by the user, but not loaded. Before using the commands from this package, the user will have to load it with thelibrarycommand, i.e.,library(car).Have I installed a specific package?Sometimes, you might want to know if you have already installed a specific package. Lets say we want to check if we have installed the package boot. Instead of checking the entire list of installed packages, we can do the following.a-installed.packages()packages-a,1 is.element(boot, packages)1 TRUEHow can I add or delete packages?Any package that does not appear in the installed packages matrix must be installed and loaded before its functions can be used. A package can be installed usinginstall.packages(). A package can be removed usingremove.packages().What packages are available?The list of available R packages is constantly growing. The actual list can be obtained usingavailable.packages(). This returns a matrix with a row for each package.p = 2.5.0), stats, car (= 2.0-1), Formula (= 0.2-0),nlmtest, sandwich, strucchange, survival, zoo statsAGSDest AGSDest 1.0 NA ldbounds NA AICcmodavg AICcmodavg 1.11 NA NA NA LinkingToACCLMA NA ADGofTest NA AER NA AGSDest NA AICcmodavg NA Suggests ACCLMA NA ADGofTest NA AER boot, dynlm, effects, foreign, ineq, KernSmooth, lattice,nMASS, mlogit, nlme, nnet, np, plm, pscl, quantreg, ROCR,nsampleSelection, scatterplot3d, systemfit, rgl, truncreg,ntseries, urcaAGSDest NA AICcmodavg lme4, MASS, nlme, nnet Enhances OS_type License File Repository ACCLMA NA NA GPL-2 NA /bin/windows/contrib/2.11ADGofTest NA NA GPL NA /bin/windows/contrib/2.11AER NA NA GPL-2 NA /bin/windows/contrib/2.11AGSDest NA NA GPL (= 2) NA /bin/windows/contrib/2.11AICcmodavg NA NA GPL (= 2 ) NA /bin/windows/contrib/2.11These first five (of 2,553) available packages illustrate that the package names are often acronyms and rarely reveal what the package functions do. A list of the packages available throughCRANincluding a short package description can be found atCRANs Contributed Packages page.What functions and datasets are in a package?It is easy to access some quick documentation for a package from R with thehelpcommand. This opens an R window with package information followed by a list of functions and datasets.help(package=MASS)Once a package is loaded, the help command can also be used with all functions and datasets listed here,e.g.help(Null).How to cite this pageReport an error on this page or leave a commentThe content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.来源于:/tech/support/research/software-and-programming/common-languages/r-basics/r-faq/对包的 相关操作!What version of R do I run?You can find this information by runningR.version.stringat the R prompt:# get version of R R.version.string1 R version 2.13.2 (2011-09-30)What R packages do I already have?To get a list of installed packages, executelibrary()at the R prompt. R will first list all the packages installed in your local R directory and then it will list all of the packages installed globally on your system:# list all R packages installed library()Packages in library /usr1/scv/username/R/x86_64-unknown-linux-gnu-library/2.13:combinatcombinatorics utilitiesproftoolsOutput Processing Tools for Rrgl 3D visualization device system (OpenGL)Packages in library /usr/local/IT/R-2.13.2/lib64/R/library:KernSmooth Functions for kernel smoothing for Wand & Jones (1995)MASS Support Functions and Datasets for Venables and Ripleys MASS How can I find additional information about R packages I have?If you would like to get additional information about the installed packages you can access it usinginstalled.packages()at the R prompt. A more concise output can be obtained runninginstalled.packages() ,c(1,3,4). This will include the name of the package, its version and “priority” information:# Obtain information about installed R packages installed.packages() ,c(1,3,4) Package Version Prioritycombinat combinat 0.0-8 NArgl rgl 0.92.879 NAMASS MASS 7.3-14 recommendedMatrix Matrix 0.9996875-3 recommendedbase base 2.13.2 base. If priority is“base”, the package is already loaded into your workspace, so all its functions are available upon opening R. If priority is“recommended”, then the package was installed with base R. If priority is“NA”, then the package is an optional package. Both “recommended” and “NA” packages have to be loaded usinglibrary()command before using them.How do I install a new package?Method 1: Install from CRAN directly. A package can be installed usinginstall.packages(package name)at the R prompt. You can also provide a path name to install R package at a specific location:install.packages(package name, lib=/my/own/R-packages/). If thelib=option is omitted it defaults to the first directory in.libPaths().# Install rgl package install.packages(rgl)Installing package(s) into /usr1/scv/username/R/x86_64-unknown-linux-gnu-library/2.13(as lib is unspecified)- Please select a CRAN mirror for use in this session -Loading Tcl/Tk interface . done. Method 2: Install from source. At the Linux prompt download the R packagemy_package.tar.gzand useR CMD INSTALLcommand:# Download rgl packagescc1% wget /src/contrib/rgl_0.92.894.tar.gz-2012-11-16 09:08:34- /src/contrib/rgl_0.92.894.tar.gzResolving . 7Connecting to |7|:80. connected. # Install rgl packagescc1% R CMD INSTALL rgl_0.92.894.tar.gz* installing to library /usr1/scv/koleinik/R/x86_64-unknown-linux-gnu-library/2.13* installing *source* package rgl .checking for gcc. gcc -std=gnu99.* building package indices .* testing if installed package can be loaded* DONE (rgl) Note:Regardless of the method you used to install the package, it has to be loaded into the workspace before you can actually use it. To load it, typelibrary(my_package)at the R prompt.How can I remove a package?A package can be removed usingremove.packages(package name)at the R prompt:# Remove rgl package remove.packages(rgl)Removing package(s) from /usr1/scv/koleinik/R/x86_64-unknown-linux-gnu-library/2.13(as lib is unspecified) How can I check if there are updates for the installed packages?To check for updates runold.packages()at the R prompt. You will be asked to specify CRAN mirror. Then R will list all the projects that have a newer version.# Check for updates old.packages() - Please select a CRAN mirror for use in this session -Loading Tcl/Tk interface . done . How can I update a package?To install a newer version of a package runupdate.packages()at the R prompt. R will list all the packages that have newer versions and will offer to download and install the updates.# Update all outdated packages update.packages() .# Update rgl package only update.packages( oldPkgs=rgl ) - Please select a CRAN mirror for use in this session -Loading Tcl/Tk interface . done . Where are my R packages installed?You can view the list of the packages along with their paths runningpath.package()at the R prompt:# Where are my R packages installed path.package() 1 /usr/local/IT/R-2.13.2/lib64/R/library/stats2 /usr/local/IT/R-2.13.2/lib64/R/library/graphics3 /usr/local/IT/R-2.13.2/lib64/R/library/grDevices What is the default path R uses to install new packages?You can view the default path R is using to install new packages running.libPaths()at the R prompt:# Get the default R paths .libPaths()1 /usr1/scv/username/R/x86_64-unknown-linux-gnu-library/2.132 /usr/local/IT/R-2.13.2/lib64/R/library How can I run another R version installed on the SCC?You can list and run non-default versions of R available , running the following commands at the Linux prompt:# List all R versions installed on scc1scc1% ls -l /usr/local/IT/R*/usr/local/IT/R-2.10.1:total 44lrwxrwxrwx 1 root root 11 May 17 2010 R - ./R-2.10.1/-rw-r-r- 1 root root 1938 May 17 2010 READMEdrwxr-xr-x 2 root root 4096 May 17 2010 bin/ . # Run a non-default R versionscc1% /usr/local/IT/R-2.12.2/bin/RHow can I install another version of R?You can install another R version locally in your home directory.First, create a local directory where you plan to install R. Find the mirror site from which you would like to download R. In the example below we usemy_Ras a name of the directory used for the R installation. We will be installing version 2.15.2.Execute the following commands at the Linux prompt:# Set home directory as a current directoryscc1% cd# Create a directory where you plan to install Rscc1% mkdir my

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论