widecr: Wide format of competition results

widecrR Documentation

Wide format of competition results

Description

Functions for dealing with competition results in wide format.

Usage

is_widecr(cr_data)

as_widecr(cr_data, repair = TRUE, ...)

## S3 method for class 'widecr'
as_tibble(x, ...)

Arguments

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.

Details

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.

Value

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.

Wide format of competition results

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().

Repairing

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.

See Also

Long format

Examples

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)

as_tibble(cr_data_wide)

comperes documentation built on March 7, 2023, 7:02 p.m.