Having fun testing out the capabilities of the select_if()
function, especially learning how to bundle multiple conditionals together for column selection.
For example I can select factors that have more than x levels via this sort of code
iris %>% select_if(~is.factor(.) & nlevels(.) > 2) %>% names
[1] "Species"
But when I try to use one of the string matching functions it doesn’t work
iris %>% select_if(~is.numeric(.) & ends_with("Length")) %>% names
Error:
! `ends_with()` must be used within a *selecting* function.
ℹ See ?tidyselect::faq-selection-context for details.
The help for the select_if()
function states “Functions like starts_with(), contains() or matches() are selection helpers that only work in a selection context, e.g. dplyr::select() or the cols argument of tidyr::pivot_longer()”
But isn’t select_if
a selecting function?
Any help or solutions much appreciated