|
| 1 | +## -------------------------------------- ## |
| 2 | +# Development Difference Count Function |
| 3 | +## -------------------------------------- ## |
| 4 | + |
| 5 | +# Should be similar to `count` but instead of counting instances of elements, should count difference in those counts between two vectors |
| 6 | + |
| 7 | +## -------------------------- ## |
| 8 | +# Housekeeping ---- |
| 9 | +## -------------------------- ## |
| 10 | + |
| 11 | +# Load libraries |
| 12 | +librarian::shelf(tidyverse) |
| 13 | + |
| 14 | +# Clear environment |
| 15 | +rm(list = ls()); gc() |
| 16 | + |
| 17 | +## -------------------------- ## |
| 18 | +# Explore Area ---- |
| 19 | +## -------------------------- ## |
| 20 | + |
| 21 | +# Make two test vectors |
| 22 | +x1 <- c(1, 1, NA, "a", 1, "a", NA, "x") |
| 23 | +x2 <- c(1, NA, NA, "a", "x") |
| 24 | + |
| 25 | +# Count elements |
| 26 | +ct1 <- supportR::count(vec = x1) %>% |
| 27 | + dplyr::rename(count_one = count) |
| 28 | +ct2 <- supportR::count(vec = x2) %>% |
| 29 | + dplyr::rename(count_two = count) |
| 30 | + |
| 31 | +# Combine |
| 32 | +ixn <- dplyr::full_join(x = ct1, y = ct2, by = "value") %>% |
| 33 | + dplyr::mutate(diff = count_one - count_two) %>% |
| 34 | + dplyr::select(-dplyr::starts_with("count_")) |
| 35 | + |
| 36 | +# Check out output |
| 37 | +ixn |
| 38 | + |
| 39 | +# Clear environment |
| 40 | +rm(list = ls()); gc() |
| 41 | + |
| 42 | +## -------------------------- ## |
| 43 | +# Function Dev Area ---- |
| 44 | +## -------------------------- ## |
| 45 | + |
| 46 | +# Define function |
| 47 | +count_diff <- function(vec1, vec2, what = NULL){ |
| 48 | + |
| 49 | + # If 'what' is NULL, make it the unique contents of both vectors |
| 50 | + if(is.null(what)){ what <- unique(c(vec1, vec2)) } |
| 51 | + |
| 52 | + # Count all items in each vector |
| 53 | + n1 <- supportR::count(vec = vec1) |
| 54 | + n2 <- supportR::count(vec = vec2) |
| 55 | + |
| 56 | + # Combine these data objects |
| 57 | + full_n <- dplyr::full_join(x = n1, y = n2, by = "value") %>% |
| 58 | + # Calculate difference |
| 59 | + dplyr::mutate(diff = count.x - count.y) %>% |
| 60 | + # Rename columns |
| 61 | + dplyr::rename(vec1_count = count.x, |
| 62 | + vec2_count = count.y) |
| 63 | + |
| 64 | + # Subset to only desired items |
| 65 | + sub_n <- dplyr::filter(.data = full_n, value %in% what) |
| 66 | + |
| 67 | + # Return that |
| 68 | + return(sub_n) } |
| 69 | + |
| 70 | +# Define test vectors |
| 71 | +x1 <- c(1, 1, NA, "a", 1, "a", NA, "x") |
| 72 | +x2 <- c(1, NA, NA, "a", "x") |
| 73 | + |
| 74 | +# Invoke function to get difference of all things |
| 75 | +count_diff(vec1 = x1, vec2 = x2, what = NULL) |
| 76 | + |
| 77 | +# Invoke again to count difference in a specific element |
| 78 | +count_diff(vec1 = x1, vec2 = x2, what = NA) |
| 79 | +count_diff(vec1 = x1, vec2 = x2, what = "a") |
| 80 | + |
| 81 | +# Clear environment |
| 82 | +rm(list = ls()); gc() |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +# End ---- |
0 commit comments