R语言的R包及其使用

Python015

R语言的R包及其使用,第1张

1、通过选择菜单:

程序包->安装程序包->在弹出的对话框中,选择你要安装的包,然后确定。

2、使用命令

install.packages(package_name,dir)

package_name:是指定要安装的包名,请注意大小写。

dir:包安装的路径。默认情况下是安装在..\library 文件夹中的。可以通过本参数来进行修改,来选择安装的文件夹。

3、本地来安装

如果你已经下载的相应的包的压缩文件,则可以在本地来进行安装。请注意在windows、unix、macOS操作系统下安装文件的后缀名是不一样的:

1)linux环境编译运行:tar.gz文件

2)windows 环境编译运行 :.zip文件

3)MacOSg环境编译运行:.tgz文件

注:包安装好后,并不可以直接使用,如果在使用包中相关的函数,必须每次使用前包加载内存中。通过library(package_name)来完成。 包安装后,如果要使用包的功能。必须先把包加载到内存中(默认情况下,R启动后默认加载基本包),加载包命令:

Library(“包名”)

Require(“包名”) 1、查看包帮忙

library(help=package_name)

主要内容包括:例如:包名、作者、版本、更新时间、功能描述、开源协议、存储位置、主要的函数

help(package = package_name)

主要内容包括:包的内置所有函数,是更为详细的帮助文档

2、查看当前环境哪些包加载

find.package() 或者 .path.package()

3、移除包出内存

detach()

4、把其它包的数据加载到内存中

data(dsname, package=package_name)

5、查看这个包里的包有数据

data( package=package_name)

6、列出所有安装的包

library()

R objects that reside in other R objects can require a lot of typing to access. For example, to refer to a variable x in a dataframe df , one could type df$x . This is no problem when the dataframe and variable names are short, but can become burdensome when longer names or repeated references are required, or objects in complicated structures must be accessed.

The attach() function in R can be used to make objects within dataframes accessible in R with fewer keystrokes. As an example:

... then detach() the dataset to clean up after ourselves.

之后,用命令 detach() 结束使用数据集。

users are cautioned that if there is already a variable called cesd in the local workspace, issuing attach(ds) , may not mean that cesd references ds$cesd . Name conflicts of this type are a common problem with attach() and care should be taken to avoid them.

The help page for attach() notes that attach can lead to confusion . The Google R Style Manual provides clear advice on this point, providing the following advice about attach() : The possibilities for creating errors when using attach are numerous. Avoid it.

So what options exist for those who decide to go cold turkey?

那么,有哪些应对方法呢?

(Also note the within() function, which is similar to with() , but returns a modified object.)

Some examples may be helpful:

比如下面这个例子:

In short, there’s never an actual need to use attach() , using it can lead to confusion or errors, and alternatives exists that avoid the problems. We recommend against it.

详细语法可以在R中输入 ??with 查看。

文献参考: https://www.r-bloggers.com/to-attach-or-not-attach-that-is-the-question/