Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does R use partial matching?

Tags:

I know that for a list, partial matching is done when indexing using the basic operators $ and [[. For example:

ll <- list(yy=1) ll$y [1] 1 

But I am still an R newbie and this is new for me, partial matching of function arguments:

h <- function(xx=2)xx h(x=2) [1] 2 

I want to understand how this works. What is the mechanism behind it? Does this have any side effects? I want understand how can someone test if the xx argument was given?

Edit after Andrie comment:

Internally R uses pmatch algorithm to match argument, here an example how this works:

 pmatch("me",   c("mean", "median", "mode")) # error multiple partial matches [1] NA > pmatch("mo",   c("mean", "median", "mode")) # mo match mode match here [1] 3 

But why R has such feature? What is the basic idea behind of partial unique matching?

like image 276
agstudy Avatar asked Jan 04 '13 08:01

agstudy


People also ask

What is partial matching in R?

(A partial match occurs if the whole of the element of x matches the beginning of the element of table .) Finally, all remaining elements of x are regarded as unmatched. In addition, an empty string can match nothing, not even an exact match to an empty string.

What is partially matching?

A partial matching is a collection A of arcs with the requirement that each vertex is contained in at most one arc.

How do I find partial strings in R?

The charmatch() is a built-in R function that finds matches between two arguments. To do a Partial String Matching in R, use the charmatch() function. The charmatch() function accepts three arguments and returns the integer vector of the same length as input.

What is argument matching in R?

14.3 Argument Matching rm is a logical indicating whether missing values should be removed or not.


1 Answers

Partial matching exists to save you typing long argument names. The danger with it is that functions may gain additional arguments later on which conflict with your partial match. This means that it is only suitable for interactive use – if you are writing code that will stick around for a long time (to go in a package, for example) then you should always write the full argument name. The other problem is that by abbreviating an argument name, you can make your code less readable.

Two common good uses are:

  1. len instead of length.out with the seq (or seq.int) function.

  2. all instead of all.names with the ls function.

Compare:

seq.int(0, 1, len = 11)  seq.int(0, 1, length.out = 11)  ls(all = TRUE) ls(all.names = TRUE) 

In both of these cases, the code is just about as easy to read with the shortened argument names, and the functions are old and stable enough that another argument with a conflicting name is unlikely to be added.

A better solution for saving on typing is, rather than using abbreviated names, to use auto-completion of variable and argument names. R GUI and RStudio support this using the TAB key, and Architect supports this using CTRL+Space.

like image 200
Richie Cotton Avatar answered Nov 12 '22 16:11

Richie Cotton