nrow_subset_linter {lintr} | R Documentation |
Block usage of nrow(subset(x, .))
Description
Using nrow(subset(x, condition))
to count the instances where condition
applies inefficiently requires doing a full subset of x
just to
count the number of rows in the resulting subset.
There are a number of equivalent expressions that don't require the full
subset, e.g. with(x, sum(condition))
(or, more generically,
with(x, sum(condition, na.rm = TRUE))
).
Usage
nrow_subset_linter()
Tags
best_practices, consistency, efficiency
See Also
linters for a complete list of linters available in lintr.
Examples
# will produce lints
lint(
text = "nrow(subset(x, is_treatment))",
linters = nrow_subset_linter()
)
lint(
text = "nrow(filter(x, is_treatment))",
linters = nrow_subset_linter()
)
lint(
text = "x %>% filter(x, is_treatment) %>% nrow()",
linters = nrow_subset_linter()
)
# okay
lint(
text = "with(x, sum(is_treatment, na.rm = TRUE))",
linters = nrow_subset_linter()
)
[Package lintr version 3.2.0 Index]