Skip to contents

Exclude columns matching a pattern and the resulting duplicates

Usage

exclude(data, match)

Arguments

data

A dataframe.

match

A character vector. If length > 1, the union of the matches is taken.

For starts_with(), ends_with(), and contains() this is an exact match. For matches() this is a regular expression, and can be a stringr pattern.

Value

A dataframe excluding the matching columns and duplicates.

See also

Examples

library(tibble)

# Excludes columns along with all its duplicates
data <- tibble(x = 1, y = 1:2)
data
#> # A tibble: 2 × 2
#>       x     y
#>   <dbl> <int>
#> 1     1     1
#> 2     1     2
data |> exclude("y")
#> # A tibble: 1 × 1
#>       x
#>   <dbl>
#> 1     1

# Columns are matched as a regular expression
data <- tibble(x = 1, yz = 1:2, zy = 1)
data
#> # A tibble: 2 × 3
#>       x    yz    zy
#>   <dbl> <int> <dbl>
#> 1     1     1     1
#> 2     1     2     1
data |> exclude("y")
#> # A tibble: 1 × 1
#>       x
#>   <dbl>
#> 1     1
data |> exclude("y$")
#> # A tibble: 2 × 2
#>       x    yz
#>   <dbl> <int>
#> 1     1     1
#> 2     1     2



# With a 'tilt_profile' excludes at both levels in a single step

product <- company <- tibble(companies_id = 1, y = "a", z = 1)
result <- tilt_profile(nest_levels(product, company))
result |> class()
#> [1] "tilt_profile" "tbl_df"       "tbl"          "data.frame"  
result
#> # A tibble: 1 × 3
#>   companies_id product          company         
#> *        <dbl> <list>           <list>          
#> 1            1 <tibble [1 × 2]> <tibble [1 × 2]>

out <- result |> exclude("y")
out |> unnest_product()
#> # A tibble: 1 × 2
#>   companies_id     z
#>          <dbl> <dbl>
#> 1            1     1
out |> unnest_company()
#> # A tibble: 1 × 2
#>   companies_id     z
#>          <dbl> <dbl>
#> 1            1     1