Order a dataframe based on the position of one row

I am trying to decode some poker hands. In poker, each player plays in a named position, and each position has its own strategy. I need to label each position to study each strategy.
There are 6 possible positions:
Position 1 = Big blind
Position 2= Small Blind
Position 3= Button
Position 4= Cutoff
Position 5 = Middle Position
Position 6= Early Position

I have something like this:

df<-data.frame(text=c("Seat 1: Player M ($0.49 in chips)",
                  "Seat 2: Player X ($2.05 in chips)",
                  "Seat 3: Player L ($2.08 in chips)",
                  "Seat 5: Player J ($1.96 in chips)",
                  "Seat 6: Player W ($0.43 in chips)",
                  "Player X: posts small blind",
                  "Player L: posts big blind"))

In this example, players are ordered how they are seated (note that a Seat might be empty, like Seat 4)
I would like to number them based on their position relative to the strategy named before, so in this case i would have:
Player L woulb be "1" (Big Blind),
Player X would be "2" (Small Blind)
Player M would be "3" (Button)
Player W would be "4" (Cutoff)
Player J would be "5" (Middle Position)
Since there are only 5 players, there would not be position 6.

I am not sure how to do this. I think I would need to:

  1. Reorder them, based on that relative position of the one that has ": posts big blind" , that would be the number one.
  2. Number the rows based on that order.

There is any way to order based on a relative position?

expected<-data.frame(text=c("Seat 3: Player L ($2.08 in chips)",
                  "Seat 2: Player X ($2.05 in chips)",
                  "Seat 1: Player M ($0.49 in chips)",
                  "Seat 6: Player W ($0.43 in chips)",
                  "Seat 5: Player J ($1.96 in chips)"),

Hi @cereghetti,
I think this task will require that you get the text information into a tidy data frame before sorting the positions, and allocating the position names. This code may give you a start:

dat <- "
Seat Player Chips Post
1 M 0.49 NA
2 X 2.05 Small_blind
3 L 2.08 Big_blind
5 J 1.96 NA
6 W 0.43 NA"

datin <- read.table(text=dat, header=TRUE, strip.white=TRUE)

# Allocate "Position"
datin$Position <- NA
datin$Position[datin$Post %in% "Big_blind"] <- 1
datin$Position[datin$Post %in% "Small_blind"] <- 2

remaining_positions <- c(3:length(datin$Seat))
datin$Position[is.na(datin$Position)] <- remaining_positions

# Sort on Position
#>   Seat Player Chips        Post Position
#> 3    3      L  2.08   Big_blind        1
#> 2    2      X  2.05 Small_blind        2
#> 1    1      M  0.49        <NA>        3
#> 4    5      J  1.96        <NA>        4
#> 5    6      W  0.43        <NA>        5

pos_names <- c("Big_blind","Small_blind","Button","Middle","Cutoff","Early")
datin$Position_name <- factor(datin$Position,

#>   Seat Player Chips        Post Position Position_name
#> 1    1      M  0.49        <NA>        3        Button
#> 2    2      X  2.05 Small_blind        2   Small_blind
#> 3    3      L  2.08   Big_blind        1     Big_blind
#> 4    5      J  1.96        <NA>        4        Middle
#> 5    6      W  0.43        <NA>        5        Cutoff

Created on 2021-01-18 by the reprex package (v0.3.0)
Warning: I am NOT a poker player so may have misunderstood the details. Also, this simple code may fail with variable seat numbers. If the input data is only available in the text format shown then a parser function will need to be written .

Thank you This gave me a nice start, I could addapt it to my needs. You saved me!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.