dplyr::filter() ignore case

rlookup is :--

locations pop

Chelmsford 123

Why does this work ?

.location="Chelmsford"
.locid <- rlookup %>% filter(locations==.location, ignore.case=TRUE)

but this doesnt

.location="CHELMSFORD"
.locid <- rlookup %>% filter(locations==.location, ignore.case=TRUE)

?

ignore.case isn't an argument in dplyr::filter().

You could force both sides of the logical expression to be the same case?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

dat = tibble(locations = "Chelmsford",
             pop = 123)

# correct case

.location = "Chelmsford"

dat |> filter(locations == .location)
#> # A tibble: 1 x 2
#>   locations    pop
#>   <chr>      <dbl>
#> 1 Chelmsford   123

# incorrect cases

.location = "CHELMSFORD"

dat |> filter(toupper(locations) == toupper(.location))
#> # A tibble: 1 x 2
#>   locations    pop
#>   <chr>      <dbl>
#> 1 Chelmsford   123

.location = "chelmsford"

dat |> filter(toupper(locations) == toupper(.location))
#> # A tibble: 1 x 2
#>   locations    pop
#>   <chr>      <dbl>
#> 1 Chelmsford   123

.location = "ChElMsFoRd"

dat |> filter(toupper(locations) == toupper(.location))
#> # A tibble: 1 x 2
#>   locations    pop
#>   <chr>      <dbl>
#> 1 Chelmsford   123

Created on 2022-03-07 by the reprex package (v2.0.1)

Thanks for your help

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.