Functions for dealing with competition results in wide format.
is_widecr(cr_data)
as_widecr(cr_data, repair = TRUE, ...)
# S3 method for widecr
as_tibble(x, ...)
cr_data | Data of competition results (convertible to tabular). |
---|---|
repair | Whether to repair input. |
... | Additional arguments to be passed to or from methods. |
x | Object to be converted to tibble. |
is_widecr()
returns TRUE
if its argument is appropriate object
of class widecr
: it should inherit classes widecr
, tbl_df
(in other
words, to be tibble) and have complete pairs of
"player"-"score" columns where pair is detected by digits after strings
"player" and "score" respectively. Columns of "player" and "score" types
shouldn't have any extra symbols except type name and digits after it. All
other columns are considered as "extra columns".
as_widecr()
returns an object of class widecr
.
as_tibble() applied to widecr
object drops widecr
class.
as_widecr()
is S3 method for converting data to widecr
. When
using default method if repair
is TRUE
it also tries to fix
possible problems (see "Repairing"). If repair
is FALSE
it converts
cr_data
to tibble and adds widecr
class to it.
When applying as_widecr()
to proper (check via is_longcr()
is made)
longcr
object, conversion is made:
All columns except "game", "player" and "score" are dropped.
Conversion from long to wide format is made. The number of "player"-"score"
pairs is taken as the maximum number of players in game. If not all games are
played between the same number of players then there will be NA
's in some
pairs. Column game
is preserved in output and is used for arranging in
increasing order.
For appropriate widecr
objects as_widecr
returns its input and
throws error otherwise.
It is assumed that competition consists from multiple games (matches, comparisons, etc.). One game can consist only from constant number of players. Inside a game all players are treated equally. In every game every player has some score: the value of arbitrary nature that fully characterizes player's performance in particular game (in most cases it is some numeric value).
widecr
inherits from tibble
. Data should be organized in pairs of columns
"player"-"score". Identifier of a pair should go after respective keyword and
consist only from digits. For example: player1, score1, player2, score2.
Order doesn't matter. Extra columns are allowed.
To account for R standard string ordering, identifiers of pairs should be formatted with leading zeros (when appropriate). For example: player01, score01, ..., player10, score10.
Column game
for game identifier is optional. If present it will be used in
conversion to longcr
format via as_longcr()
.
Option repair = TRUE
(default) in as_widecr()
means that its result is
going to be repaired with following actions:
Detect columns with names containing "player" or "score" (ignoring case). All other columns are treated as "extra".
Extract first occurrence of "player" or "score" (ignoring case) from names of detected columns. Everything after extracted word is treated as identifier of "player"-"score" pair.
Convert these identifiers to numeric form with
as.integer(as.factor(...))
.
Convert identifiers once again to character form with possible leading zeros (to account for R standard string ordering).
Spread pairs to appropriate columns with possible column adding (which
were missed in original pairs based on information of pair identifier) with
NA_integer_
.
Note that if there is column game
(exactly matched) it is placed as
first column.
Note that the order (and numeration) of pairs can change.
cr_data <- data.frame(
playerA = 1:10,
playerB = 2:11,
scoreC = 11:20,
scoreB = 12:21,
otherColumn = 101:110
)
cr_data_wide <- as_widecr(cr_data, repair = TRUE)
#>
#>
#>
#>
#> #>
#>
is_widecr(cr_data_wide)
#> [1] TRUE#> # A tibble: 10 x 7
#> player1 score1 player2 score2 player3 score3 otherColumn
#> <int> <int> <int> <int> <int> <int> <int>
#> 1 1 NA 2 12 NA 11 101
#> 2 2 NA 3 13 NA 12 102
#> 3 3 NA 4 14 NA 13 103
#> 4 4 NA 5 15 NA 14 104
#> 5 5 NA 6 16 NA 15 105
#> 6 6 NA 7 17 NA 16 106
#> 7 7 NA 8 18 NA 17 107
#> 8 8 NA 9 19 NA 18 108
#> 9 9 NA 10 20 NA 19 109
#> 10 10 NA 11 21 NA 20 110