Perform analysis on a correspondence table using its associated classifications to generate various statistics. It checks the validity of the input data, identifies components, calculates correspondence types, and creates summary tables.

analyseCorrespondenceTable(
  AB,
  Aname = NULL,
  Bname = NULL,
  A = NULL,
  B = NULL,
  longestAcodeOnly = FALSE,
  longestBcodeOnly = FALSE
)

Arguments

AB

A mandatory argument containing a data frame provided by the user with the correspondence table data. The table must contain at least two columns containing the two classifications A code and B code. The name of the two columns is irrelevant.

Aname

Optional. A character string specifying the name of the column in `AB` that should be used as the source classification (`Acode`). If `NULL`, the function assumes that the first column of `AB` represents the source classification.

Bname

Optional. A character string specifying the name of the column in `AB` that should be used as the target classification (`Bcode`). If `NULL`, the function assumes that the second column of `AB` represents the target classification.

A

A data frame containing the source classification data with an "Acode" column. Can be NULL if no source classification table is provided.

B

A data frame containing the target classification data with a "Bcode" column. Can be NULL if no target classification table is provided.

longestAcodeOnly

Logical. If TRUE, source classification data are filtered based on "Acode" retaining only the maximum length, i.e. the lowest-level Acodes in the correspondence table.

longestBcodeOnly

Logical. If TRUE, target classification data are filtered based on "Bcode" retaining only the maximum length, i.e. the lowest-level Bcodes in the correspondence table.

Value

A list containing two data frames: Inventory and Analysis. Inventory contains statistics related to components, correspondence types, and source/target positions. Analysis contains statistics for each class in the correspondence table.

Examples

# Use data from the folder extdata/test

AB = read.csv(system.file("extdata/test", "ExempleAnnexe.csv", package = "correspondenceTables"))
head(AB)
#>   ClassA ClassB
#> 1    C01    D04
#> 2    C01    D21
#> 3    C01    D13
#> 4    C01    D20
#> 5    C01    D11
#> 6    C02    D22

result <- analyseCorrespondenceTable(
  AB = AB,
  Aname = NULL,
  Bname = NULL,
  A  = NULL,
  B  = NULL,
  longestAcodeOnly          = FALSE,
  longestBcodeOnly          = FALSE
)

print(result$Inventory)
#>     Component CorrespondenceType                   SourcePositions
#> 1 Component 1                M:M           C01, C06, C12, C21, C24
#> 2 Component 2                M:M C02, C09, C11, C17, C18, C22, C25
#> 3 Component 3                M:M                C03, C05, C08, C15
#> 4 Component 4                M:M                     C04, C16, C20
#> 5 Component 5                M:M                     C07, C14, C23
#> 6 Component 6                1:M                               C10
#> 7 Component 7                1:1                               C13
#> 8 Component 8                M:1                          C19, C26
#>           TargetPositions nSourcePositions nTargetPositions
#> 1 D04, D21, D13, D20, D11                5                5
#> 2           D22, D03, D12                7                3
#> 3 D07, D08, D05, D02, D17                4                5
#> 4           D24, D19, D10                3                3
#> 5                D16, D06                3                2
#> 6           D01, D18, D09                1                3
#> 7                     D15                1                1
#> 8                     D14                2                1
print(result$Analysis)
#>    ClassA ClassB nTargetClasses   SourceToTargetMapping nSourceClasses
#> 1     C01    D04              5 D04, D21, D13, D20, D11              1
#> 2     C01    D11              5 D04, D21, D13, D20, D11              1
#> 3     C01    D13              5 D04, D21, D13, D20, D11              1
#> 4     C01    D20              5 D04, D21, D13, D20, D11              1
#> 5     C01    D21              5 D04, D21, D13, D20, D11              5
#> 6     C02    D03              2                D22, D03              3
#> 7     C02    D22              2                D22, D03              3
#> 8     C03    D07              2                D07, D08              2
#> 9     C03    D08              2                D07, D08              2
#> 10    C04    D19              2                D24, D19              2
#> 11    C04    D24              2                D24, D19              2
#> 12    C05    D05              2                D05, D07              1
#> 13    C05    D07              2                D05, D07              2
#> 14    C06    D21              1                     D21              5
#> 15    C07    D16              1                     D16              2
#> 16    C08    D02              2                D02, D08              2
#> 17    C08    D08              2                D02, D08              2
#> 18    C09    D12              1                     D12              3
#> 19    C10    D01              3           D01, D18, D09              1
#> 20    C10    D09              3           D01, D18, D09              1
#> 21    C10    D18              3           D01, D18, D09              1
#> 22    C11    D03              1                     D03              3
#> 23    C12    D21              1                     D21              5
#> 24    C13    D15              1                     D15              1
#> 25    C14    D06              2                D06, D16              2
#> 26    C14    D16              2                D06, D16              2
#> 27    C15    D02              2                D02, D17              2
#> 28    C15    D17              2                D02, D17              1
#> 29    C16    D24              1                     D24              2
#> 30    C17    D12              1                     D12              3
#> 31    C18    D03              1                     D03              3
#> 32    C19    D14              1                     D14              2
#> 33    C20    D10              2                D19, D10              1
#> 34    C20    D19              2                D19, D10              2
#> 35    C21    D21              1                     D21              5
#> 36    C22    D22              1                     D22              3
#> 37    C23    D06              1                     D06              2
#> 38    C24    D21              1                     D21              5
#> 39    C25    D12              2                D22, D12              3
#> 40    C25    D22              2                D22, D12              3
#> 41    C26    D14              1                     D14              2
#>      TargetToSourceMapping
#> 1                      C01
#> 2                      C01
#> 3                      C01
#> 4                      C01
#> 5  C01, C06, C12, C21, C24
#> 6            C02, C11, C18
#> 7            C02, C22, C25
#> 8                 C03, C05
#> 9                 C03, C08
#> 10                C04, C20
#> 11                C04, C16
#> 12                     C05
#> 13                C03, C05
#> 14 C01, C06, C12, C21, C24
#> 15                C07, C14
#> 16                C08, C15
#> 17                C03, C08
#> 18           C09, C17, C25
#> 19                     C10
#> 20                     C10
#> 21                     C10
#> 22           C02, C11, C18
#> 23 C01, C06, C12, C21, C24
#> 24                     C13
#> 25                C14, C23
#> 26                C07, C14
#> 27                C08, C15
#> 28                     C15
#> 29                C04, C16
#> 30           C09, C17, C25
#> 31           C02, C11, C18
#> 32                C19, C26
#> 33                     C20
#> 34                C04, C20
#> 35 C01, C06, C12, C21, C24
#> 36           C02, C22, C25
#> 37                C14, C23
#> 38 C01, C06, C12, C21, C24
#> 39           C09, C17, C25
#> 40           C02, C22, C25
#> 41                C19, C26

# the name of the columns does not matter:
AB_mod = AB
colnames(AB_mod) = c("a", "b")

result_mod <- analyseCorrespondenceTable(
  AB = AB_mod,
  Aname = NULL,
  Bname = NULL,
  A  = NULL,
  B  = NULL,
  longestAcodeOnly          = FALSE,
  longestBcodeOnly          = FALSE
)

print(result_mod$Inventory)
#>     Component CorrespondenceType                   SourcePositions
#> 1 Component 1                M:M           C01, C06, C12, C21, C24
#> 2 Component 2                M:M C02, C09, C11, C17, C18, C22, C25
#> 3 Component 3                M:M                C03, C05, C08, C15
#> 4 Component 4                M:M                     C04, C16, C20
#> 5 Component 5                M:M                     C07, C14, C23
#> 6 Component 6                1:M                               C10
#> 7 Component 7                1:1                               C13
#> 8 Component 8                M:1                          C19, C26
#>           TargetPositions nSourcePositions nTargetPositions
#> 1 D04, D21, D13, D20, D11                5                5
#> 2           D22, D03, D12                7                3
#> 3 D07, D08, D05, D02, D17                4                5
#> 4           D24, D19, D10                3                3
#> 5                D16, D06                3                2
#> 6           D01, D18, D09                1                3
#> 7                     D15                1                1
#> 8                     D14                2                1
print(result_mod$Analysis)
#>      a   b nTargetClasses   SourceToTargetMapping nSourceClasses
#> 1  C01 D04              5 D04, D21, D13, D20, D11              1
#> 2  C01 D11              5 D04, D21, D13, D20, D11              1
#> 3  C01 D13              5 D04, D21, D13, D20, D11              1
#> 4  C01 D20              5 D04, D21, D13, D20, D11              1
#> 5  C01 D21              5 D04, D21, D13, D20, D11              5
#> 6  C02 D03              2                D22, D03              3
#> 7  C02 D22              2                D22, D03              3
#> 8  C03 D07              2                D07, D08              2
#> 9  C03 D08              2                D07, D08              2
#> 10 C04 D19              2                D24, D19              2
#> 11 C04 D24              2                D24, D19              2
#> 12 C05 D05              2                D05, D07              1
#> 13 C05 D07              2                D05, D07              2
#> 14 C06 D21              1                     D21              5
#> 15 C07 D16              1                     D16              2
#> 16 C08 D02              2                D02, D08              2
#> 17 C08 D08              2                D02, D08              2
#> 18 C09 D12              1                     D12              3
#> 19 C10 D01              3           D01, D18, D09              1
#> 20 C10 D09              3           D01, D18, D09              1
#> 21 C10 D18              3           D01, D18, D09              1
#> 22 C11 D03              1                     D03              3
#> 23 C12 D21              1                     D21              5
#> 24 C13 D15              1                     D15              1
#> 25 C14 D06              2                D06, D16              2
#> 26 C14 D16              2                D06, D16              2
#> 27 C15 D02              2                D02, D17              2
#> 28 C15 D17              2                D02, D17              1
#> 29 C16 D24              1                     D24              2
#> 30 C17 D12              1                     D12              3
#> 31 C18 D03              1                     D03              3
#> 32 C19 D14              1                     D14              2
#> 33 C20 D10              2                D19, D10              1
#> 34 C20 D19              2                D19, D10              2
#> 35 C21 D21              1                     D21              5
#> 36 C22 D22              1                     D22              3
#> 37 C23 D06              1                     D06              2
#> 38 C24 D21              1                     D21              5
#> 39 C25 D12              2                D22, D12              3
#> 40 C25 D22              2                D22, D12              3
#> 41 C26 D14              1                     D14              2
#>      TargetToSourceMapping
#> 1                      C01
#> 2                      C01
#> 3                      C01
#> 4                      C01
#> 5  C01, C06, C12, C21, C24
#> 6            C02, C11, C18
#> 7            C02, C22, C25
#> 8                 C03, C05
#> 9                 C03, C08
#> 10                C04, C20
#> 11                C04, C16
#> 12                     C05
#> 13                C03, C05
#> 14 C01, C06, C12, C21, C24
#> 15                C07, C14
#> 16                C08, C15
#> 17                C03, C08
#> 18           C09, C17, C25
#> 19                     C10
#> 20                     C10
#> 21                     C10
#> 22           C02, C11, C18
#> 23 C01, C06, C12, C21, C24
#> 24                     C13
#> 25                C14, C23
#> 26                C07, C14
#> 27                C08, C15
#> 28                     C15
#> 29                C04, C16
#> 30           C09, C17, C25
#> 31           C02, C11, C18
#> 32                C19, C26
#> 33                     C20
#> 34                C04, C20
#> 35 C01, C06, C12, C21, C24
#> 36           C02, C22, C25
#> 37                C14, C23
#> 38 C01, C06, C12, C21, C24
#> 39           C09, C17, C25
#> 40           C02, C22, C25
#> 41                C19, C26


# examples with using Aname and Bname

# fist we add two more column to the same dataset
set.seed(123)
AB_mod2 = AB_mod

AB_mod2 <- cbind(new_first_col = sample(1:100, nrow(AB_mod2), replace = TRUE), AB_mod2)
AB_mod2 <- cbind(
  AB_mod2[1:2], 
  new_middle_col = sample(1:100, nrow(AB_mod2), replace = TRUE),
  AB_mod2[3:ncol(AB_mod2)]
)
colnames(AB_mod2)
#> [1] "new_first_col"  "a"              "new_middle_col" "b"             

result_mod2 <- analyseCorrespondenceTable(
  AB = AB_mod2,
  Aname = "a",
  Bname = "b",
  A  = NULL,
  B  = NULL,
  longestAcodeOnly          = FALSE,
  longestBcodeOnly          = FALSE
)

print(result_mod2$Inventory)
#>     Component CorrespondenceType                   SourcePositions
#> 1 Component 1                M:M           C01, C06, C12, C21, C24
#> 2 Component 2                M:M C02, C09, C11, C17, C18, C22, C25
#> 3 Component 3                M:M                C03, C05, C08, C15
#> 4 Component 4                M:M                     C04, C16, C20
#> 5 Component 5                M:M                     C07, C14, C23
#> 6 Component 6                1:M                               C10
#> 7 Component 7                1:1                               C13
#> 8 Component 8                M:1                          C19, C26
#>           TargetPositions nSourcePositions nTargetPositions
#> 1 D04, D21, D13, D20, D11                5                5
#> 2           D22, D03, D12                7                3
#> 3 D07, D08, D05, D02, D17                4                5
#> 4           D24, D19, D10                3                3
#> 5                D16, D06                3                2
#> 6           D01, D18, D09                1                3
#> 7                     D15                1                1
#> 8                     D14                2                1
print(result_mod2$Analysis)
#>      a   b nTargetClasses   SourceToTargetMapping nSourceClasses
#> 1  C01 D04              5 D04, D21, D13, D20, D11              1
#> 2  C01 D11              5 D04, D21, D13, D20, D11              1
#> 3  C01 D13              5 D04, D21, D13, D20, D11              1
#> 4  C01 D20              5 D04, D21, D13, D20, D11              1
#> 5  C01 D21              5 D04, D21, D13, D20, D11              5
#> 6  C02 D03              2                D22, D03              3
#> 7  C02 D22              2                D22, D03              3
#> 8  C03 D07              2                D07, D08              2
#> 9  C03 D08              2                D07, D08              2
#> 10 C04 D19              2                D24, D19              2
#> 11 C04 D24              2                D24, D19              2
#> 12 C05 D05              2                D05, D07              1
#> 13 C05 D07              2                D05, D07              2
#> 14 C06 D21              1                     D21              5
#> 15 C07 D16              1                     D16              2
#> 16 C08 D02              2                D02, D08              2
#> 17 C08 D08              2                D02, D08              2
#> 18 C09 D12              1                     D12              3
#> 19 C10 D01              3           D01, D18, D09              1
#> 20 C10 D09              3           D01, D18, D09              1
#> 21 C10 D18              3           D01, D18, D09              1
#> 22 C11 D03              1                     D03              3
#> 23 C12 D21              1                     D21              5
#> 24 C13 D15              1                     D15              1
#> 25 C14 D06              2                D06, D16              2
#> 26 C14 D16              2                D06, D16              2
#> 27 C15 D02              2                D02, D17              2
#> 28 C15 D17              2                D02, D17              1
#> 29 C16 D24              1                     D24              2
#> 30 C17 D12              1                     D12              3
#> 31 C18 D03              1                     D03              3
#> 32 C19 D14              1                     D14              2
#> 33 C20 D10              2                D19, D10              1
#> 34 C20 D19              2                D19, D10              2
#> 35 C21 D21              1                     D21              5
#> 36 C22 D22              1                     D22              3
#> 37 C23 D06              1                     D06              2
#> 38 C24 D21              1                     D21              5
#> 39 C25 D12              2                D22, D12              3
#> 40 C25 D22              2                D22, D12              3
#> 41 C26 D14              1                     D14              2
#>      TargetToSourceMapping new_first_col new_middle_col
#> 1                      C01            31             53
#> 2                      C01            67             89
#> 3                      C01            51             96
#> 4                      C01            14             38
#> 5  C01, C06, C12, C21, C24            79             27
#> 6            C02, C11, C18            50             93
#> 7            C02, C22, C25            42             34
#> 8                 C03, C05            43             69
#> 9                 C03, C08            14             72
#> 10                C04, C20            90             63
#> 11                C04, C16            25             76
#> 12                     C05            91             13
#> 13                C03, C05            69             82
#> 14 C01, C06, C12, C21, C24            91             97
#> 15                C07, C14            57             91
#> 16                C08, C15            92             25
#> 17                C03, C08             9             38
#> 18           C09, C17, C25            93             21
#> 19                     C10            99             79
#> 20                     C10            26             47
#> 21                     C10            72             41
#> 22           C02, C11, C18             7             90
#> 23 C01, C06, C12, C21, C24            42             60
#> 24                     C13             9             95
#> 25                C14, C23            83             16
#> 26                C07, C14            36             94
#> 27                C08, C15            78              6
#> 28                     C15            81             72
#> 29                C04, C16            43             86
#> 30           C09, C17, C25            76             86
#> 31           C02, C11, C18            15             39
#> 32                C19, C26            32             31
#> 33                     C20            41             34
#> 34                C04, C20             9             50
#> 35 C01, C06, C12, C21, C24            74              4
#> 36           C02, C22, C25            23             13
#> 37                C14, C23            27             69
#> 38 C01, C06, C12, C21, C24            60             25
#> 39           C09, C17, C25             7             22
#> 40           C02, C22, C25            53             52
#> 41                C19, C26             7             81

# check if we obtained the same results in all the 3 examples

first = identical(result$Inventory, result_mod$Inventory) 
second = identical(result_mod$Inventory, result_mod2$Inventory)
first && second
#> [1] TRUE
# TRUE

# the following won't work as we changed column names in AB_mod, 
# and added new columns in AB_mod2 but the results are the same.
first = identical(result$Analysis, result_mod$Analysis)
second = identical(result_mod$Analysis, result_mod2$Analysis)
first && second
#> [1] FALSE