R:在两个不同的包中有两个名称相同的函数

时间:2021-12-19 08:34:16

I need to load to R packages : tseries and chron

我需要装载到R包:tseries和chron

Both have a function named is.weekend

它们都有一个名为is.weekend的函数

I always have in my environment the function from the second package I loaded.

在我的环境中,我总是有从第二个包中加载的函数。

How can I access always the function from, let say, chron ?

我如何总是从,比如说,chron访问函数?

2 个解决方案

#1


66  

You have probably already noticed that the order of loading the packages makes a difference, i.e. the package that gets loaded last will mask the functions in packages loaded earlier.

您可能已经注意到,加载包的顺序会有所不同,例如,最后加载的包将掩盖先前加载的包中的函数。

To specify the package that you want to use, the syntax is:

要指定要使用的包,语法是:

chron::is.weekend()
tseries::is.weekend()

In other words, use packagename::functionname()

换句话说,使用packagename::functionname()

In addition, if you know that you will always want to use the function in chron, you can define your own function as follows:

此外,如果你知道你一直想在chron中使用这个函数,你可以定义你自己的函数如下:

is.weekend <- chron::is.weekend    #EDIT

#2


2  

library(chron)
is.weekend.chron <- is.weekend
library(tseries)

then you can call is.weekend for the tseries version or is.weekend.chron for the chron version

然后你可以打电话给is。周末为tseries版本或is.weekend。chron为chron版本

#1


66  

You have probably already noticed that the order of loading the packages makes a difference, i.e. the package that gets loaded last will mask the functions in packages loaded earlier.

您可能已经注意到,加载包的顺序会有所不同,例如,最后加载的包将掩盖先前加载的包中的函数。

To specify the package that you want to use, the syntax is:

要指定要使用的包,语法是:

chron::is.weekend()
tseries::is.weekend()

In other words, use packagename::functionname()

换句话说,使用packagename::functionname()

In addition, if you know that you will always want to use the function in chron, you can define your own function as follows:

此外,如果你知道你一直想在chron中使用这个函数,你可以定义你自己的函数如下:

is.weekend <- chron::is.weekend    #EDIT

#2


2  

library(chron)
is.weekend.chron <- is.weekend
library(tseries)

then you can call is.weekend for the tseries version or is.weekend.chron for the chron version

然后你可以打电话给is。周末为tseries版本或is.weekend。chron为chron版本