R 中的双冒号 (::) 是什么? [英] What are the double colons (::) in R?

查看:48
本文介绍了R 中的双冒号 (::) 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Rbloggers 中的教程,发现双冒号的使用,我在网上查看,但找不到对它们使用的解释.这是它们的使用示例.

I am following a tutorial in Rbloggers and found the use of double colons, I looked online, but I couldn't find an explanation for their use. Here is an example of their use.

df <- dplyr::data_frame(
  year = c(2015, NA, NA, NA), 
  trt = c("A", NA, "B", NA)
)

我知道它创建了一个数据框,但我不明白它们的目的.

I understand it creates a data frame but I don't understand their purpose.

推荐答案

正如您现在可能已经查找过帮助页面那样,使用 :: 有助于访问特定包中的确切功能.当您加载 dplyr 时,您可能会收到如下消息..

As you probably have looked up the help page by now usage of :: helps to access the exact function from that specific package. When you load dplyr you probably got a message as follows..

The following objects are masked from ‘package:base’:
       intersect, setdiff, setequal, union

因此,例如,如果您想使用 dplyr 或基础包中的 intersect 函数,您需要使用 :: 双冒号指定.用法如下

So, for instance, if you would like to use intersect function from dplyr or base package, you need to specify using the :: double colons. Usage will be as follows

mtcars$model <- rownames(mtcars)
first <- mtcars[1:20, ]
second <- mtcars[10:20, ]
dplyr::intersect(first, second)
base::intersect(first, second)

更新:添加了附加说明

注意:您加载库的顺序决定了特定功能的优先访问权限.不同包的开发者倾向于使用相同的函数名.但是,当 R 遇到一个函数时,它会按顺序运行特定会话已加载的不同库.您可以通过运行 (.packages())

Note: The sequence you load libraries determine the preferential access of the specific functions. Developers of different package tend to use same function names. However, when R encounters a function, it runs through the different libraries that particular session has loaded in a sequential manner. You can check the packages in a session by running (.packages())

 [1] "tidyr"      "data.table" "dplyr"      "stats"     
 [5] "graphics"   "grDevices"  "utils"      "datasets"  
 [9] "methods"    "base"    

正如您在上面的示例会话中看到的,tidyr 是我加载的最后一个库,它是 r session 的第一个条目.所以,当你在代码中使用任何函数时,首先在 tidyr -> 然后 data.table -> 然后 dplyr 中搜索它上,最后查找base包.因此,在此过程中,当包之间存在函数名称重叠时,最后加载的包会屏蔽前一包.为避免这种屏蔽,您可以在 R 代码中指定查找函数的位置.因此,这里 base::intersect 将使用基础库中的函数而不是 dplyr.或者,您可以使用来避免加载完整的库.这有积极和消极的一面.阅读链接并了解更多信息.

As you can see in my example session above, tidyr is the last library I loaded, which is r session 1st entry. So, when you use any function in your code , first it is searched in tidyr -> then data.table -> then dplyr and so on, finally the base package is looked up. So, in this process when there is function name overlaps between packages the one which loaded the last masks the previous ones. To avoid this masking, you specify in R code where to look for the function. Hence, here base::intersect, will use the function from base library instead of the dplyr. Alternatively, you can use to avoid loading of complete library. There are positives and negatives with this. Read the links and learn more.

运行并检查差异.这里有一些资源供您了解.

run and check the differences. Here are some resources for you to get an understanding.

比较 library(), require(), ::

命名空间

这篇关于R 中的双冒号 (::) 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆