File size: 10,717 Bytes
2071122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

#'  Diagnostics for messy joins
#'
#'  given a data frame with two ways to group rows,
#'  summarize and give examples of situations where the mapping is not 1-1
#'
#' @param x_cols tidyselect specification of a set of columns defining objects
#' @param y_cols tidyselect specification of a set of columns defining objects
#'
#' data <- data.frame(
#' 	x=c(1,2,NA,3,4,5,6,6,6,7,7),
#' 	y=c("a",NA,"c","d","d","d","e","f","g","h","h"))
#'
#' data |> summarize_map(
#'   x_cols = x),
#'   y_cols = y))
#' X<-[x]:
#'   |X|: 7                          # number of groups
#'   |is.na.X|: 1                    # number of groups with NA in atleaset 1 col
#'   range(|x|:X): 1, 3              # size range of groups
#' Y<-[y]:
#'   |Y|: 7
#'   |is.na.Y|: 1
#'   range(|y|:Y): 1, 3
#' [X U Y]:                          # grouping by the union of xcols and ycols
#'   |X U Y|: 8
#'   |is.na.XUY|: 2
#'   range(|z|:X U Y): 1, 2
#' [X @ Y]:
#'   |X ~ Y|: 5
#'   |X:X < Y|, |Y:Y < X|: 1, 1
#'   |X:X > Y|, |Y:Y < X|: 3, 3
#' $is.na.X
#'    x y
#' 1 NA c
#'
#' $is.na.Y
#'   x    y
#' 1 2 <NA>
#'
#' $dup.XUY
#'   x y
#' 1 7 h
#' 2 7 h
#'
#' $dup.X
#'   x y
#' 1 6 e
#' 2 6 f
#' 3 6 g
#'
#' $dup.Y
#'   y x
#' 1 d 3
#' 2 d 4
#' 3 d 5
#' @export
summarize_map <- function(
  data,
  x_cols,
  y_cols,
  n_examples = 4,
  verbose = FALSE) {

    # convert column selections named vectors of column indices into data
    x_cols <- tidyselect::eval_select(rlang::enquo(x_cols), data)
    y_cols <- tidyselect::eval_select(rlang::enquo(y_cols), data)
    xUy_cols <- union(x_cols, y_cols)
    names(xUy_cols) <- names(data[xUy_cols])
    
    if(verbose) {
        cat("The following is a report of the relationship between two different ways of identifying instances\n")
    }

    # example rows
    problems <- list()

    count_xUy <- data |>
        dplyr::count(dplyr::across(tidyselect::all_of(xUy_cols))) |>
        dplyr::ungroup()
    count_x <- count_xUy |>
        dplyr::count(dplyr::across(tidyselect::all_of(names(x_cols))), name = "size") |>
        dplyr::ungroup()
    count_y <- count_xUy |>
        dplyr::count(dplyr::across(tidyselect::all_of(names(y_cols))), name = "size") |>
        dplyr::ungroup()
    
    if (verbose) {
        cat("\nProperties of X identifiers:\n")
    }
    cat("X<-[", paste(names(x_cols), collapse = ", "), "]:\n", sep = "")
    cat("  |X|: ", count_x |> stats::na.omit(method = "r") |> nrow(), sep = "")
    
    na_count <- data |>
        dplyr::select(tidyselect::all_of(x_cols)) |>
        stats::complete.cases() |>
        magrittr::not() |>
        sum()
    cat(
        ifelse(
            na_count == 0,
            "",
            paste0(" (", na_count, " NA)")),
        "\n", sep = "")

    size_dist <- count_x |>
        stats::na.omit(method = "r") |>
        dplyr::count(size) |>
        dplyr::ungroup()
    if (nrow(size_dist) < 12) {
        cat("  count*size: ",
            paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "),
            "\n", sep = "")
	} else {
            top <- 1:6
            bottom <- (nrow(size_dist) - 6+1):nrow(size_dist)
            cat("  count*size: ",
                paste(
                    size_dist$n[top],
                    size_dist$size[top], sep = "*", collapse = ", "),
                ", ... ",
                paste(
                    size_dist$n[bottom],
                    size_dist$size[bottom], sep = "*", collapse = ", "),
                "\n", sep="")
	}

    if (verbose) {
        cat("\nProperties of the Y identifiers:\n")
    }
    cat("Y<-[", paste(names(y_cols), collapse = ", "), "]:\n", sep = "")
    cat("  |Y|: ", count_y |> stats::na.omit(method = "r") |> nrow(), sep = "")
    na_count <- data |>
        dplyr:::select(tidyselect::all_of(y_cols)) |>
        stats::complete.cases() |>
        magrittr::not() |>
        sum()
    cat(ifelse(na_count == 0, "", paste0(" (", na_count, " NA)")), "\n", sep = "")
    
    size_dist <- count_y |>
        stats::na.omit(method = "r") |>
        dplyr::count(size) |>
        dplyr::ungroup()
    if (nrow(size_dist) < 12) {
        cat("  count*size: ",
            paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "),
            "\n", sep = "")
    } else {
        top <- 1:6
        bottom <- (nrow(size_dist) - 6+1):nrow(size_dist)
        cat("  count*size: ",
            paste(
                size_dist$n[top],
                size_dist$size[top], sep = "*", collapse = ", "),
            ", ... ",
            paste(
                size_dist$n[bottom],
                size_dist$size[bottom], sep = "*", collapse = ", "),
            "\n", sep="")
    }
    
    if (verbose) {
        cat("\nProperties of the intersection of union of the X and Y identifiers:\n")
    }
    cat("[X U Y]:\n")
    cat("  |X U Y|: ", count_xUy |> stats::na.omit(method = "r") |> nrow(), sep = "")
    na_count <- data |>
        dplyr:::select(!!!xUy_cols) |>
        stats::complete.cases() |>
        magrittr::not() |>
        sum()
    cat(ifelse(na_count == 0, "", paste0(" (", na_count, " NA)")), "\n", sep = "")
    
    size_dist <- count_xUy |>
        stats::na.omit(method = "r") |>
        dplyr::rename(size = n) |>
        dplyr::count(size) |>
        dplyr::ungroup()
    if (nrow(size_dist) < 12) {
        cat("  count*size: ",
            paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "),
            "\n", sep="")
    } else {
        top <- 1:6
        bottom <- (nrow(size_dist) - 6+1):nrow(size_dist)
        cat("  count*size: ",
            paste(
                size_dist$n[top],
                size_dist$size[top], sep = "*", collapse = ", "),
            ", ... ",
            paste(
                size_dist$n[bottom],
                size_dist$size[bottom], sep = "*", collapse = ", "),
            "\n", sep = "")
    }
    

    count_xUy <- count_xUy |> stats::na.omit(method = "r")
    
    if (verbose) {
        cat("Properties of the intersection of the X and Y identifiers:\n")
    }
    cat("[X @ Y]:\n")
    if (verbose) {
        cat("  Number of X and Y identifiers that are 1 to 1:\n")
    }
    cat("  |X ~ Y|: ",
        count_xUy |>
        dplyr::semi_join(
            count_x |> dplyr::filter(size == 1),
            by = names(x_cols)) |>
        dplyr::semi_join(
            count_y |> dplyr::filter(size == 1),
            by = names(y_cols)) |>
        nrow(),
        "\n", sep = "")
    
    if (verbose) {
        cat("  Number of X and Y identifiers where an X identifier maps to multiple Y identifiers:\n")
    }
    cat(
        "  |X:X < Y|, |Y:Y < X|: ",
        count_xUy |>
        dplyr::semi_join(
            count_x |> dplyr::filter(size > 1),
            by = names(x_cols)) |>
        nrow(),
        ", ",
        count_xUy |>
        dplyr::count(
            dplyr::across(tidyselect::all_of(names(x_cols))),
            name = "size") |>
        dplyr::filter(size > 1) |>
        nrow(),
        "\n", sep = "")
    
    if (verbose) {
        cat(
            "  Number of X and Y identifiers where a Y identifier maps to ",
            "multiple X identifiers:\n")
    }
    cat(
        "  |X:X > Y|, |Y:Y > X|: ",
        count_xUy |>
        dplyr::semi_join(
            count_y |>
            dplyr::filter(size > 1),
            by = names(y_cols)) |>
        nrow(),
        ", ",
        count_xUy |>
        dplyr::count(
            dplyr::across(tidyselect::all_of(names(y_cols))),
            name = "size") |>
        dplyr::filter(size > 1) |>
        nrow(),
        "\n", sep = "")
    
    #is.na.X
    ex_rows <- data |>
        dplyr:::select(tidyselect::all_of(x_cols)) |>
        stats::complete.cases() |>
        magrittr::not() |>
        which()
    if (length(ex_rows)) {
        if (!is.null(n_examples) && (n_examples < length(ex_rows))) {
            ex_rows <- ex_rows |> sample(n_examples, replace = FALSE)
        }
        problems$is.na.X <- data |>
            dplyr::slice(ex_rows) |>
            dplyr::arrange(dplyr::across(tidyselect::all_of(names(x_cols))))
    }
    
    #is.na.Y
    ex_rows <- data |>
        dplyr:::select(tidyselect::all_of(y_cols)) |>
        stats::complete.cases() |>
        magrittr::not() |>
        which()
    if (length(ex_rows)) {
        if (!is.null(n_examples) && (n_examples < length(ex_rows))) {
            ex_rows <- ex_rows |> sample(n_examples, replace = FALSE)
        }
        problems$is.na.Y <- data |>
            dplyr::slice(ex_rows) |>
            dplyr::arrange(dplyr::across(tidyselect::all_of(names(y_cols))))
    }
    
    #dup.X
    dup.X <- count_xUy |>
        dplyr::filter(n == 1) |>
        dplyr::count(
            dplyr::across(tidyselect::all_of(names(x_cols))),
            name = "size") |>
        dplyr::filter(size > 1) |>
        dplyr::ungroup() |>
        dplyr:::select(-size)
    if (nrow(dup.X) > 1) {
        if (!is.null(n_examples) && (n_examples < nrow(dup.X))) {
            dup.X <- dup.X |> dplyr::sample_n(n_examples, replace = FALSE)
        }
        problems$dup.X <- dup.X |>
            dplyr::left_join(data, by = names(x_cols)) |>
            dplyr::arrange(dplyr::across(tidyselect::all_of(names(x_cols))))
    }
    
    #dup.Y
    dup.Y <- count_xUy |>
        dplyr::filter(n == 1) |>
        dplyr::count(
            dplyr::across(tidyselect::all_of(names(y_cols))),
            name = "size") |>
        dplyr::filter(size > 1) |>
        dplyr::ungroup() |>
        dplyr:::select(-size)
    if (nrow(dup.Y) > 1) {
        if (!is.null(n_examples) && (n_examples < nrow(dup.Y))) {
            dup.Y <- dup.Y |> dplyr::sample_n(n_examples, replace = FALSE)
        }
        problems$dup.Y <- dup.Y |>
            dplyr::left_join(data, by = names(ycols)) |>
            dplyr::arrange(dplyr::across(tidyselect::all_of(names(y_cols))))
    }
    
    #dup.XUY
    dup.XUY <- count_xUy |>
        dplyr::filter(n > 1) |>
        dplyr:::select(-n)
    if (nrow(dup.XUY) > 1) {
        if (!is.null(n_examples) && (n_examples < nrow(dup.XUY))) {
            dup.XUY <- dup.XUY |> dplyr::sample_n(n_examples, replace = FALSE)
        }
        problems$dup.XUY <- dup.XUY |>
            dplyr::left_join(data, by = names(xUy_cols)) |>
            dplyr::arrange(dplyr::across(tidyselect::all_of(names(xUy_cols))))
    }
    if (verbose) {
        cat("Returned instances where:\n")
        cat("\tis.na.X: The X identifier is NA\n")
        cat("\tis.na.Y: The Y identifier is NA\n")
        cat("\tdup.X:   The X identifier is not unique\n")
        cat("\tdup.Y:   The Y identifier is not unique\n")
        cat("\tdup.XUY: The X and Y identifiers together are not unique\n")
    }
    problems
}