R中包的操作.docx

上传人:b****2 文档编号:11581913 上传时间:2023-06-01 格式:DOCX 页数:16 大小:54.13KB
下载 相关 举报
R中包的操作.docx_第1页
第1页 / 共16页
R中包的操作.docx_第2页
第2页 / 共16页
R中包的操作.docx_第3页
第3页 / 共16页
R中包的操作.docx_第4页
第4页 / 共16页
R中包的操作.docx_第5页
第5页 / 共16页
R中包的操作.docx_第6页
第6页 / 共16页
R中包的操作.docx_第7页
第7页 / 共16页
R中包的操作.docx_第8页
第8页 / 共16页
R中包的操作.docx_第9页
第9页 / 共16页
R中包的操作.docx_第10页
第10页 / 共16页
R中包的操作.docx_第11页
第11页 / 共16页
R中包的操作.docx_第12页
第12页 / 共16页
R中包的操作.docx_第13页
第13页 / 共16页
R中包的操作.docx_第14页
第14页 / 共16页
R中包的操作.docx_第15页
第15页 / 共16页
R中包的操作.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

R中包的操作.docx

《R中包的操作.docx》由会员分享,可在线阅读,更多相关《R中包的操作.docx(16页珍藏版)》请在冰点文库上搜索。

R中包的操作.docx

R中包的操作

R语言中包的操作

1.列出包所在库的路径

.libPaths()

[1]"C:

/ProgramFiles/R/R-3.0.2/library"    

列出一个特定包的安装路径:

a<-.libPaths("maps");a

2.安装包,括号里面包的名称要加英文引号,在列出的CRAN镜像站点列表中选择一个进行下载,我一般选的是China(Hefei)

install.packages("packagesname")

例如,install.packages("ggplot2")

R包的指定位置安装:

 install.packages("包名字",lib="安装目录",repos="包所在的网址))

nstall.packages("packagesname",lib=D:

\\......,repos=)

R包指定安装位置引用:

使用.libPaths()函数添加包路径:

.libPaths("myLibPath");

library("mypackage")

通过以上2步,就可以调用安装在myLibPath路径下的mypackage;

3.包的载入library()或require(),安装完包后,需要加载才能使用其中的函数,此时括号中不使用引号。

两者的不同之处在于library()载入之后不返回任何信息,而require()载入后则会返回TRUE,因此require()适合用于程序的书写。

例如

library(ggplto2)

>require(foreign)

Loadingrequiredpackage:

foreign

> is.logical(require(foreign))

[1]TRUE

4.包的更新

update.packages()

5.包的帮助信息格式如下,可以查看包中的函数以及说明

help(package="ggplot2")

6.查看本地的包

6.1查看默认加载的包,忽略基本的包

getOption("defaultPackages")

>getOption("defaultPackages")

[1]"datasets""utils""grDevices""graphics""stats""methods"

[7]"ggplot2"

6.2查看当前已经加载过的包

(.packages())

[1]"ggplot2""stats""graphics""grDevices""utils""datasets""methods""base"

6.3要显示所有可用的包

(.packages(all.available=TRUE))

>(.packages(all.available=TRUE))

  [1]"abind""agricolae""aplpack""base""bitops"

  [6]"boot""car""caTools""class""cluster"

[11]"codetools""colorRamps""colorspace""compiler""datasets"

[16]"Defaults""devtools""dichromat""digest""doBy"

[21]"e1071""effects""ellipse""evaluate""foreign"

[26]"formatR""Formula""gdata""ggplot2""ggthemes"

[31]"gmodels""gplots""graphics""grDevices""grid"

[36]"gtable""gtools""highr""Hmisc""httr"

[41]"KernSmooth""knitr""labeling""lattice""latticeExtra"

[46]"leaps""lme4""lmtest""LSD""manipulate"

[51]"markdown""MASS""Matrix""matrixcalc""memoise"

[56]"methods""mgcv""minqa""multcomp""munsell"

[61]"mvtnorm""nlme""nnet""nortest""parallel"

[66]"pixmap""plyr""proto""psych""quantmod"

[71]"Rcmdr""RColorBrewer""Rcpp""RcppEigen""RCurl"

[76]"relimp""reshape2""rgl""rJava""RODBC"

[81]"rpart""rstudio""samplesize""sandwich""scales"

[86]"schoolmath""sciplot""sem""spatial""splines"

[91]"stats""stats4""stringr""survival""tcltk"

[96]"tcltk2""TH.data""tools""TTR""utils"

[101]"VennDiagram""whisker""XLConnect""xts""zoo"

7.卸载包detach(),这是library()的反向操作,此操作主要是为了避免某些包中的函数名称相同,造成冲突,注意与library()的参数不同,detach()参数为detach(package:

包的名称),library(包的名称)。

例如

>library(ggplot2)#加载包

>(.packages())#列出当前已经加载的包

[1]"ggplot2""stats""graphics""grDevices""utils""datasets"

[7]"methods""base"

>detach(package:

ggplot2)#卸载ggplot2包

>(.packages())#列出当前已经加载的包

[1]"stats""graphics""grDevices""utils""datasets""methods"

[7]"base"

8.自定义启动时候的加载包

如果需要长期使用某个包的话,每次开启都需要输入library(),比较麻烦,因此可以让R启动时自动加载某些包。

在R的安装目录/etc/Rprofile.site加入下载语句:

例如让R启动时自动加载ggplot2包

local({old<-getOption("defaultPackages")

        options(defaultPackages=c(old,"ggplot2"))})

9.在文章中引用R软件包,例如引用ggplot2包:

citation(package="ggplot2")

Tociteggplot2inpublications,pleaseuse:

  H.Wickham.ggplot2:

elegantgraphicsfordataanalysis.SpringerNew

  York,2009.

ABibTeXentryforLaTeXusersis

  @Book{,

    author={HadleyWickham},

    title={ggplot2:

elegantgraphicsfordataanalysis},

    publisher={SpringerNewYork},

    year={2009},

    isbn={978-0-387-98140-6},

    url={http:

//had.co.nz/ggplot2/book},

  }

10.总结操作常用的命令:

查看已安装的包:

library()search():

安装好后,可以用来检验。

installed.packages()installed.packages()[1:

5,]

安装一个新包:

install.packages("packagesname")

更改包的默认安装路径:

默认路径目录为library.

install.packages("packagesname",lib="yourpath")

默认包的安装路径:

自定义一个安装包,并让第三方包都安在此目录下:

添加包的搜索路:

.libPaths(c(path1,path2))

.libPaths(c("D:

/ProgramFiles/R/R-3/library","D:

/ProgramFiles/R/Rpackages"))

去年自定义包的搜索路径:

.libPaths(c("D:

/ProgramFiles/R/R-3/library"))

卸载一个已安装的包:

(删除)

remove.packages("packagesname")

临时关闭一个包:

(不被访问)

detach("package:

names",unload=TRUE)

相似包有冲突函数:

(建议使用命名空间。

如mgcv有一个冲突函数)

mgcv:

:

functionname

查看包的版本:

installed.packages()

packageVersion("snow")

参见:

来源于:

http:

//www.ats.ucla.edu/stat/r/faq/packages.htm

HowcanImanageRpackages?

Risastatisticalsoftwaremadeupofmanyuser-writtenpackages.ThebaseversionofRthatisdownloadedallowstheusertogetstartedinR,butanyoneperformingdataanalysiswillquicklyexhaustthecapabilitiesofbaseRandneedtoinstalladditionalpackages.HerearesomebasiccommandsformanagingRpackages.

WhichpackagesdoIalreadyhave?

Toseewhatpackagesareinstalled,usethe installed.packages() command.Thiswillreturnamatrixwitharowforeachpackagethathasbeeninstalled.Below,welookatthefirst5rowsofthismatrix.

installed.packages()[1:

5,]

PackageLibPathVersionPriority

base"base""C:

/PROGRA~1/r/R-211~1.1/library""2.11.1""base"

boot"boot""C:

/PROGRA~1/r/R-211~1.1/library""1.2-42""recommended"

car"car""C:

/PROGRA~1/r/R-211~1.1/library""2.0-2"NA

class"class""C:

/PROGRA~1/r/R-211~1.1/library""7.3-2""recommended"

cluster"cluster""C:

/PROGRA~1/r/R-211~1.1/library""1.12.3""recommended"

DependsImportsLinkingTo

baseNANANA

boot"R(>=2.9.0),graphics,stats"NANA

car"R(>=2.1.1),stats,graphics,MASS,nnet,survival"NANA

class"R(>=2.5.0),stats,utils""MASS"NA

cluster"R(>=2.9.0),stats,graphics,utils"NANA

SuggestsEnhancesOS_typeLicenseBuilt

baseNANANA"PartofR2.11.1""2.11.1"

boot"survival"NANA"Unlimited""2.11.1"

car"alr3,leaps,lmtest,sandwich,mgcv,rgl"NANA"GPL(>=2)""2.11.1"

classNANANA"GPL-2|GPL-3""2.11.1"

clusterNANANA"GPL(>=2)""2.11.1"

Fromthisoutput,wewillfirstfocusonthe Package and Priority columns.The Package columngivesthenameofthepackageandthe Priority columnindicateswhatisneededtousefunctionsfromthepackage.

∙If Priority is"base",thenthepackageisalreadyinstalledandloaded,soallofitsfunctionsareavailableuponopeningR.

∙If Priority is"recommended",thenthepackagewasinstalledwithbaseR,butnotloaded.Beforeusingthecommandsfromthispackage,theuserwillhavetoloaditwiththe library command, e.g. library(boot).

∙If Priority isNA,thenthepackagewasinstalledbytheuser,butnotloaded.Beforeusingthecommandsfromthispackage,theuserwillhavetoloaditwiththe library command,i.e., library(car).

HaveIinstalledaspecificpackage?

Sometimes,youmightwanttoknowifyouhavealreadyinstalledaspecificpackage.Let'ssaywewanttocheckifwehaveinstalledthepackage"boot".Insteadofcheckingtheentirelistofinstalledpackages,wecandothefollowing.

a<-installed.packages()

packages<-a[,1]

is.element("boot",packages)

[1]TRUE

HowcanIaddordeletepackages?

Anypackagethatdoesnotappearintheinstalledpackagesmatrixmustbeinstalledandloadedbeforeitsfunctionscanbeused.Apackagecanbeinstalledusing install.packages("").Apackagecanberemovedusing remove.packages("").

Whatpackagesareavailable?

ThelistofavailableRpackagesisconstantlygrowing.Theactuallistcanbeobtainedusing available.packages().Thisreturnsamatrixwitharowforeachpackage.

p<-available.packages()

dim(p)

[1]255312

p[1:

5,]

PackageVersionPriorityDependsImports

ACCLMA"ACCLMA""1.0"NANANA

ADGofTest"ADGofTest""0.1"NANANA

AER"AER""1.1-7"NA"R(>=2.5.0),stats,car(>=2.0-1),Formula(>=0.2-0),\nlmtest,sandwich,strucchange,survival,zoo""stats"

AGSDest"AGSDest""1.0"NA"ldbounds"NA

AICcmodavg"AICcmodavg""1.11"NANANA

LinkingTo

ACCLMANA

ADGofTestNA

AERNA

AGSDestNA

AICcmodavgNA

Suggests

ACCLMANA

ADGofTestNA

AER"boot,dynlm,effects,foreign,ineq,KernSmooth,lattice,\nMASS,mlogit,nlme,nnet,np,plm,pscl,quantreg,ROCR,\nsampleSelection,scatterplot3d,systemfit,rgl,truncreg,\ntseries,urca"

AGSDestNA

AICcmodavg"lme4,MASS,nlme,nnet"

EnhancesOS_typeLicenseFileRepository

ACCLMANANA"GPL-2"NA"http:

//cran.stat.ucla.edu/bin/windows/contrib/2.11"

ADGofTestNANA"GPL"NA"http:

//cran.stat.ucla.edu/bin/windows/contrib/2.11"

AERNANA"GPL-2"NA"http:

//cran.stat.ucla.edu/bin/windows/contrib/2.11"

AGSDestNANA"GPL(>=2)"NA"http:

//cran.stat.ucla.edu/bin/windows/contrib/2.11"

AICcmodavgNANA"GPL(>=2)"NA"http:

//cran.stat.ucla.edu/bin/windows/contrib/2.11"

Thesefirstfive(of2,553)availablepackagesillustratethatthepackagenamesareoftenacronymsandrarelyrevealwhatthepackagefunctionsdo.Alistofthepackagesavailablethrough CRAN includingashortpackagedescriptioncanbefoundat CRAN'sContributedPackagespage.

Whatfunctionsanddatasetsareinapackage?

ItiseasytoaccesssomequickdocumentationforapackagefromRwiththe help command.ThisopensanRwindowwithpackageinformationfollowedbyalistoffunctionsanddatasets.

help(package="MASS")

Onceapackageisloaded,thehelpcommandcanalsobeusedwithallfunctionsanddatasetslistedhere, e.g. help(Null).

Howtocitethispage

Reportanerroronthispageorleaveacomment

Thecontentofthiswebsiteshouldnotbeconstruedasanendorsementofanyparticularwebsite,book,orsoftwareproductbytheUniversityofCalifornia.

来源于:

http:

//www.bu.edu/tech/support/research/software-and-programming/common-languages/r-basics/r-faq/对包的相关操作!

WhatversionofRdoIrun?

Youcanfindthisinformationbyrunning R.version.string attheRprompt:

#getversionofR

>R.version.string

[1]Rversion2.13.2(2011-09-30)

WhatRpackagesdoIalreadyhave?

Togetalistofinstalledpackages,execute library() attheRprompt.RwillfirstlistallthepackagesinstalledinyourlocalRdirectoryandt

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2