[R] Puzzled over "partial"
Richard O'Keefe
r@oknz @end|ng |rom gm@||@com
Tue Jul 27 11:36:05 CEST 2021
In this context, "partial" is not the name of any function or package in R.
It is just the name of a parameter. And its meaning, which is specific to
sort(), is spelled out in the documentation for sort:
> ?sort
...
If ‘partial’ is not ‘NULL’, it is taken to contain indices of
elements of the result which are to be placed in their correct
positions in the sorted array by partial sorting. For each of the
result values in a specified position, any values smaller than
that one are guaranteed to have a smaller index in the sorted
array and any values which are greater are guaranteed to have a
bigger index in the sorted array. (This is included for
efficiency, and many of the options are not available for partial
sorting. It is only substantially more efficient if ‘partial’ has
a handful of elements, and a full sort is done (a Quicksort if
possible) if there are more than 10.) Names are discarded for
partial sorting.
Suppose you had the question
IF the vector x were sorted, what would its 5th element be?
You could answer that by doing
sort(x)[5[
but that's more work than you really need.
sort(x, partial=5)[5]
rearranges x to c(less.than.x.5, x.5, greater.than.x.5). This is called
partial sorting, If you wanted the quartiles, then
sort(x, partial=c(p,q,r))[c(p,q,r)]
will do the job, where p, q, r are the positions where the quartiles
would end up if complete sorting were done.
It's an efficiency hack for computing quantiles, basically.
If you are interested, you could look up
https://en.wikipedia.org/wiki/Quickselect
On Mon, 26 Jul 2021 at 23:54, Nick Wray <nickmwray using gmail.com> wrote:
>
> Hello I am puzzled about the use or status of "partial" in R. years ago
> I found a little piece of code which gives the pth largest number in a
> vector:
> x<-c(5,4,7,2,6,9)
> n <- length(x)
> p<-4
> sort(x,partial=n-(n-p))[n-(n-p)]
> This works fine, although I have tried playing around with the code and
> don't understand what "partial" is doing here.
> However, wanted to work out what was going on, so I looked for "partial in
> r" on t'internet and got this site:Partial apply a function, filling in
> some arguments. — partial • purrr (tidyverse.org)
> <https://purrr.tidyverse.org/reference/partial.html#:~:text=Source%3A%20R%2Fpartial.R%20Partial%20function%20application%20allows%20you%20to,that%20an%20argument%20can%20only%20be%20partialised%20once.>
> Examples:
> # Partial is designed to replace the use of anonymous functions for #
> filling in function arguments. Instead of: compact1 <- function(x) discard
> <https://purrr.tidyverse.org/reference/keep.html>(x, is.null) # we can
> write: compact2 <- partial(discard, .p = is.null) # partial() works fine
> with functions that do non-standard # evaluation my_long_variable <- 1:10
> plot2 <- partial(plot, my_long_variable) plot2()
> when i tried to run the examples on this site I got error messages - R
> (studio) did not recognise the "partial" function here. The site did not
> say that I needed a particular package to run the "partial" function.
> Are there essentially two different things in R both described as "partial"
> but which are actually different entities?
> Thanks for any elucidation
> Nick Wray
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list