Convert a three-column data frame in which the third column is a code representing whether the item in column 1 won, lost or (if applicable) drew over/with the item in column 2, to a dataframe with counts (suitable for use in btdata)

codes_to_counts(df, codes)

Arguments

df

A three-column data frame. Each row represents a comparison between two items. The first and second columns are the names of the first and second items respectively. The third column gives a code for which won. See Details and Examples.

codes

A numeric vector or character vector, of length two or three (depending on whether there are ties.) The first and second element gives the codes used if the first or second item won respectively. If there are ties, the third element gives the code used in that case. See Details and Examples.

Value

A four-column data frame where the first two columns are the name of the first and second item. The third and fourth column gives the wins count for the first and second item respectively: 1 for a win, 0 for a loss, and 0.5 each for a draw. This data frame is in the correct format to be passed to btdata

Details

This function is needed in the BradleyTerryScalable workflow when the user data is stored in a three-column data frame where each row is a comparison between two items, and where the third column is NOT a count of the number of times the item in the first column beat the item in the second column. Rather, it could be that the third column is a code for which of the two items won (including the possibility of a tie), for example "W1", "W2", "D". Or else, it could be that the third column gives the score only in relation to the first item, e.g. 1 for a win, 0 for a loss or 0.5 for a draw without there anywhere in the table being the corresponding record for the second item (i.e. respectively 0 for a loss, 1 for a win and 0.5 for a draw.)

See also

btdata

Examples

first <- c("A", "A", "B", "A") second <- c("B", "B", "C", "C") df1 <- data.frame(player1 = first, player2 = second, code = c("W1", "W2", "D", "D")) codes_to_counts(df1, c("W1", "W2", "D"))
#> # A tibble: 4 x 4 #> player1 player2 item1wins item2wins #> <fctr> <fctr> <dbl> <dbl> #> 1 A B 1.0 0.0 #> 2 A B 0.0 1.0 #> 3 B C 0.5 0.5 #> 4 A C 0.5 0.5
df2 <- data.frame(item1 = first, item2 = second, result = c(0, 1, 1, .5)) codes_to_counts(df2, c(1, 0, .5))
#> # A tibble: 4 x 4 #> item1 item2 item1wins item2wins #> <fctr> <fctr> <dbl> <dbl> #> 1 A B 0.0 1.0 #> 2 A B 1.0 0.0 #> 3 B C 1.0 0.0 #> 4 A C 0.5 0.5
df3 <- data.frame(player1 = first, player2 = second, which_won = c(1,2,2,1)) codes_to_counts(df3, c(1,2))
#> # A tibble: 4 x 4 #> player1 player2 item1wins item2wins #> <fctr> <fctr> <dbl> <dbl> #> 1 A B 1 0 #> 2 A B 0 1 #> 3 B C 0 1 #> 4 A C 1 0
codes_to_counts(BradleyTerryScalable::toy_data, c("W1", "W2", "D"))
#> # A tibble: 17 x 4 #> player1 player2 item1wins item2wins #> <chr> <chr> <dbl> <dbl> #> 1 Cyd Amy 1.0 0.0 #> 2 Amy Ben 0.5 0.5 #> 3 Ben Eve 0.0 1.0 #> 4 Cyd Dan 0.0 1.0 #> 5 Ben Dan 0.5 0.5 #> 6 Dan Eve 0.0 1.0 #> 7 Fin Eve 0.0 1.0 #> 8 Fin Gal 0.0 1.0 #> 9 Fin Han 0.0 1.0 #> 10 Eve Gal 1.0 0.0 #> 11 Fin Gal 0.5 0.5 #> 12 Han Gal 1.0 0.0 #> 13 Han Gal 0.0 1.0 #> 14 Amy Dan 1.0 0.0 #> 15 Cyd Amy 1.0 0.0 #> 16 Ben Dan 0.5 0.5 #> 17 Dan Amy 0.0 1.0