Skip to content

Commit f701f23

Browse files
authored
* New exclude() helps remove columns and resulting duplicated rows (#772)
1 parent 25e346d commit f701f23

File tree

8 files changed

+166
-0
lines changed

8 files changed

+166
-0
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(exclude,data.frame)
4+
S3method(exclude,tilt_profile)
35
S3method(join_to,data.frame)
46
S3method(join_to,tilt_profile)
57
S3method(rowid,default)
@@ -23,6 +25,7 @@ export(example_dictionary)
2325
export(example_raw_companies)
2426
export(example_raw_ipr)
2527
export(example_raw_weo)
28+
export(exclude)
2629
export(extdata_path)
2730
export(is_tilt_profile)
2831
export(istr)

R/exclude.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#' Exclude columns matching a pattern and the resulting duplicates
2+
#'
3+
#' @param data A dataframe.
4+
#' @inheritParams tidyselect::matches
5+
#'
6+
#' @return A dataframe excluding the matching columns and duplicates.
7+
#' @export
8+
#' @family helpers
9+
#'
10+
#' @examples
11+
#' library(tibble)
12+
#'
13+
#' # Excludes columns along with all its duplicates
14+
#' data <- tibble(x = 1, y = 1:2)
15+
#' data
16+
#' data |> exclude("y")
17+
#'
18+
#' # Columns are matched as a regular expression
19+
#' data <- tibble(x = 1, yz = 1:2, zy = 1)
20+
#' data
21+
#' data |> exclude("y")
22+
#' data |> exclude("y$")
23+
#'
24+
#'
25+
#'
26+
#' # With a 'tilt_profile' excludes at both levels in a single step
27+
#'
28+
#' product <- company <- tibble(companies_id = 1, y = "a", z = 1)
29+
#' result <- tilt_profile(nest_levels(product, company))
30+
#' result |> class()
31+
#' result
32+
#'
33+
#' out <- result |> exclude("y")
34+
#' out |> unnest_product()
35+
#' out |> unnest_company()
36+
exclude <- function(data, match) {
37+
UseMethod("exclude")
38+
}
39+
40+
#' @export
41+
exclude.data.frame <- function(data, match) {
42+
out <- select(data, -matches(match))
43+
44+
no_match <- rlang::is_empty(names_diff(data, out))
45+
if (no_match) {
46+
return(out)
47+
} else {
48+
distinct(out)
49+
}
50+
}
51+
52+
#' @export
53+
exclude.tilt_profile <- function(data, match) {
54+
product <- exclude(unnest_product(data), match)
55+
company <- exclude(unnest_company(data), match)
56+
result <- nest_levels(product, company)
57+
tilt_profile(result)
58+
}
59+
60+
names_diff <- function(x, y) {
61+
setdiff(names(x), names(y))
62+
}

man/exclude.Rd

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/jitter_range.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/join_to.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/summarize_range.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/unnest_product.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-exclude.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test_that("if matches a column, excludes the column and duplicated rows", {
2+
data <- tibble(x = c(1, 1), y = x)
3+
4+
out <- exclude(data, "y")
5+
6+
expect_false(hasName(out, "y"))
7+
expect_true(nrow(out) < nrow(data))
8+
})
9+
10+
test_that("if doesn't match any column, yields the inpt data", {
11+
data <- tibble(x = c(1, 1), y = x)
12+
13+
out <- exclude(data, "unmatched")
14+
15+
expect_equal(out, data)
16+
})
17+
18+
test_that("with a tilt_profile yields a tilt_profile", {
19+
product <- company <- tibble(companies_id = c(1, 1), y = companies_id)
20+
result <- tilt_profile(nest_levels(product, company))
21+
22+
out <- exclude(result, "y")
23+
24+
expect_s3_class(out, "tilt_profile")
25+
})
26+
27+
test_that("with a tilt_profile excludes at both levels", {
28+
product <- company <- tibble(companies_id = c(1, 1), y = companies_id)
29+
result <- tilt_profile(nest_levels(product, company))
30+
31+
out <- exclude(result, "y")
32+
33+
expect_false(hasName(unnest_product(out), "y"))
34+
expect_false(hasName(unnest_company(out), "y"))
35+
36+
expect_true(nrow(unnest_product(out)) < nrow(product))
37+
expect_true(nrow(unnest_company(out)) < nrow(company))
38+
})

0 commit comments

Comments
 (0)