Skip to content

Code using startsWith recommended by string_boundary_linter is not functionally identical when data has NAs #1376

@bersbersbers

Description

@bersbersbers
strs <- c("AB", "CD", NA)

# Use startsWith() to detect a fixed initial substring. Doing so is more readable and more efficient.
grepl("^A", strs)  # [1]  TRUE FALSE FALSE

startsWith(strs, "A")  # [1]  TRUE FALSE    NA

I would expect that the warning gives enough details to either

  • suggest a working solution that considers NAs the same as grepl (I am not aware of any short in-line way), or
  • a warning that NA will require special treatment.

I fear that this necessary special treatment will be bad for readability, anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions