I'm trying to use mutate_if
or select_if
, etc, verbs with column names within the predicate function.
See example below:
> btest <- data.frame(
+ sjr_first = c('1','2','3',NA, NA, '6'),
+ jcr_first = c('1','2','3',NA, NA, '6'),
+ sjr_second = LETTERS[1:6],
+ jcr_second = LETTERS[1:6],
+ sjr_third = as.character(seq(6)),
+ jcr_fourth = seq(6) + 5,
+ stringsAsFactors = FALSE)
>
> btest %>% select_if(.predicate = ~ str_match(names(.), 'jcr'))
Error in selected[[i]] <- eval_tidy(.p(column, ...)) :
replacement has length zero
I'm aware I could use btest %>% select_at(vars(dplyr::matches('jcr')))
but my goal here is actually to combine the column name condition with another condition (e.g. is.numeric
) using mutate_if()
to operate on a subset of my columns. However I'm not sure how to get the first part with the name matching to work...