Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the double colons (::) in R?

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.

like image 204
Luis Candanedo Avatar asked Feb 06 '16 12:02

Luis Candanedo


People also ask

What does the :: operator in R do?

If you know exactly which package contains the object desired then you can reference it directly using the :: operator. Simply place the package name before the operator and the name of the object after the operator to retrieve it. operator (notice the extra colon).

What does :: mean in R?

In other words ::: is used to directly access a member of a package that is internal (i.e. not exported from the NAMESPACE). See this related question: R: calling a function from a namespace.

What do colons do in R?

Colon operator (":") in R is a function that generates regular sequences. It is most commonly used in for loops, to index and to create a vector with increasing or decreasing sequence. It is a binary operator i.e. it takes two arguments.

What does double colon mean in logic?

The double colon ( :: ) may refer to: an analogy symbolism operator, in logic and mathematics. a notation for equality of ratios. a scope resolution operator, in computer programming languages.

What is a double in R programming?

What is a double in R? The two most common numeric classes used in R are integer and double (for double precision floating point numbers). R automatically converts between these two classes when needed for mathematical purposes. As a result, it’s feasible to use R and perform analyzes for years without specifying these differences.

What does the double colon operator do in JavaScript?

The double colon operator allows you to specify the specific function you want: Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers.

Why do different R packages have different functions with different names?

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 ())

Can we use double square brackets to access columns in R?

If we search for “double square brackets in R” we come across this tutorial, which shows us that the double square brackets, i.e. [ []], can be used to directly access columns:


2 Answers

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 

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) 

Update: Added additional explanation

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"     

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.

Compare library(), require(), ::

Namespace

like image 161
user5249203 Avatar answered Oct 04 '22 16:10

user5249203


There may be multiple functions with the same name in multiple packages. The double colon operator allows you to specify the specific function you want:

package::functionname 
like image 21
CPhil Avatar answered Oct 04 '22 15:10

CPhil