setdiff_col_match: Set Difference between Overlapping Columns

Description Usage Arguments See Also Examples

View source: R/diffs.R

Description

Preemptively select for columns that overlap between 2 dataframes before calling the 'setdiff()' function.

Compare column names and positions for 2 dataframes.

Usage

1
2
3

Arguments

x

A dataframe or tibble.

y

A dataframe or tibble.

See Also

map,reduce select,reexports

map,reduce select,reexports

Other diff functions: setdiff_nrow()

Other diff functions: setdiff_nrow()

Examples

  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
library(tidyverse)
test_data <- 
        tibble(
                Group = sample(c("Apple", "Pear"), size = 10, replace = TRUE),
                A     = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE),
                B     = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE),
                C     = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 10, replace = TRUE),
                D     = sample(c(NA, TRUE, FALSE), size = 10, replace = TRUE)
        ) %>%
        dplyr::mutate(E = B)
        

test_data2 <- dplyr::bind_rows(test_data[1:5,],
                               tibble(
                                               Group = sample(c("Apple", "Pear"), size = 5, replace = TRUE),
                                               A     = sample(c(NA_integer_, 1:3), size = 5, replace = TRUE),
                                               B     = sample(c(NA_integer_, 4:6), size = 5, replace = TRUE),
                                               C     = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 5, replace = TRUE),
                                               D     = sample(c(NA, TRUE, FALSE), size = 5, replace = TRUE)
                                       ) %>%
                                       dplyr::mutate(E = B))


# Rows difference
setdiff_nrow(x = test_data,
             y = test_data2)

setdiff_nrow(x = test_data2,
             y = test_data)

# Setdiff with a Column Match
setdiff_col_match(x = test_data,
                  y = test_data2 %>%
                        dplyr::mutate(F = 1:10) %>%
                        dplyr::mutate(G = 11:20))

setdiff_col_match(x = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20),
                  y = test_data)

setdiff_col_match(x = test_data,
                  y = test_data2 %>%
                          rename(F = E))

setdiff_col_match(y = test_data,
                  x = test_data2 %>%
                          rename(F = E))

# Compare Columns
compare_cols(x = test_data,
             y = test_data2)

compare_cols(x = test_data,
                  y = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20))

compare_cols(x = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20),
                  y = test_data)


compare_cols(x = test_data,
                  y = test_data2 %>%
                          rename(F = E))

compare_cols(y = test_data,
                  x = test_data2 %>%
                          rename(F = E))
library(tidyverse)
test_data <- 
        tibble(
                Group = sample(c("Apple", "Pear"), size = 10, replace = TRUE),
                A     = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE),
                B     = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE),
                C     = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 10, replace = TRUE),
                D     = sample(c(NA, TRUE, FALSE), size = 10, replace = TRUE)
        ) %>%
        dplyr::mutate(E = B)
        

test_data2 <- dplyr::bind_rows(test_data[1:5,],
                               tibble(
                                               Group = sample(c("Apple", "Pear"), size = 5, replace = TRUE),
                                               A     = sample(c(NA_integer_, 1:3), size = 5, replace = TRUE),
                                               B     = sample(c(NA_integer_, 4:6), size = 5, replace = TRUE),
                                               C     = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 5, replace = TRUE),
                                               D     = sample(c(NA, TRUE, FALSE), size = 5, replace = TRUE)
                                       ) %>%
                                       dplyr::mutate(E = B))


# Rows difference
setdiff_nrow(x = test_data,
             y = test_data2)

setdiff_nrow(x = test_data2,
             y = test_data)

# Setdiff with a Column Match
setdiff_col_match(x = test_data,
                  y = test_data2 %>%
                        dplyr::mutate(F = 1:10) %>%
                        dplyr::mutate(G = 11:20))

setdiff_col_match(x = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20),
                  y = test_data)

setdiff_col_match(x = test_data,
                  y = test_data2 %>%
                          rename(F = E))

setdiff_col_match(y = test_data,
                  x = test_data2 %>%
                          rename(F = E))

# Compare Columns
compare_cols(x = test_data,
             y = test_data2)

compare_cols(x = test_data,
                  y = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20))

compare_cols(x = test_data2 %>%
                          dplyr::mutate(F = 1:10) %>%
                          dplyr::mutate(G = 11:20),
                  y = test_data)


compare_cols(x = test_data,
                  y = test_data2 %>%
                          rename(F = E))

compare_cols(y = test_data,
                  x = test_data2 %>%
                          rename(F = E))

meerapatelmd/rubix documentation built on Sept. 5, 2021, 8:30 p.m.