[R] Sorting based a custom sorting function

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Dec 14 10:51:41 CET 2023


This sounds suspiciously like homework (which is off-topic... see the Posting Guide), and you haven't indicated how you plan to encode your poker hands, and most core features of other languages are possible in R so if you really understand these other techniques and R then you should be able to do this already.

If this is not homework, then please show your work so far instead of showing a completely different example.

On December 14, 2023 12:00:12 AM PST, "Martin Møller Skarbiniks Pedersen" <traxplayer using gmail.com> wrote:
>Hi,
>
>  I need to sort a data.frame based on a custom sorting function.
>  It is easy in many languages but I can't find a way to do it in R.
>
>  In many cases I could just use an ordered factor but my data.frame
>contains poker hands and
>I need to rank these hands. I already got a function that compares two hands.
>
>Here is a MRE (Minimal, Reproducible Example):
>
>
>df <- data.frame(person = c("Alice", "Bob", "Charlie"), value =
>c("Medium", "Small", "Large"))
>
># 0 means equal, -1 means left before right, 1 means right before left
>custom_sort <- function(left, right) {
>  if (left == right) return(0)
>  if (left == "Small") return(-1)
>  if (left == "Medium" & right == "Large") return(-1)
>  return(1)
>}
>
>#  sort df according to custom_soft
># expect output is a data.frame:
>#     name   size
># 1     Bob Medium
># 2   Alice  Small
># 3 Charlie  Large
>
>In this simple case I can just use an ordered factor but what about
>the poker hands situation?
>
>Regards
>Martin
>
>______________________________________________
>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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list