Skip to contents

lengths() is a function that was added to base R in version 3.2.0 to get the length of each element of a list. It is equivalent to sapply(x, length), but faster and more readable.

Usage

lengths_linter()

See also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = "sapply(x, length)",
  linters = lengths_linter()
)
#> <text>:1:1: warning: [lengths_linter] Use lengths() to find the length of each element in a list.
#> sapply(x, length)
#> ^~~~~~~~~~~~~~~~~

lint(
  text = "vapply(x, length, integer(1L))",
  linters = lengths_linter()
)
#> <text>:1:1: warning: [lengths_linter] Use lengths() to find the length of each element in a list.
#> vapply(x, length, integer(1L))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lint(
  text = "purrr::map_int(x, length)",
  linters = lengths_linter()
)
#> <text>:1:1: warning: [lengths_linter] Use lengths() to find the length of each element in a list.
#> purrr::map_int(x, length)
#> ^~~~~~~~~~~~~~~~~~~~~~~~~

# okay
lint(
  text = "lengths(x)",
  linters = lengths_linter()
)
#>  No lints found.