So when cleaning data I have to apply a function that removes all rows in which a certain column ends with the string "Total".
I tried to apply it the str_detect to all columns, but it did not work.
Then, I thought I would create a function. I'm also in the process of learning to get better at creating functions. But I can somehow not wrap my head around them. The normal code looks like this:
D3 <- D3 %>%
dplyr::filter(!stringr::str_detect(institution, "Total$"))
I have several variables, where institution is supposed to be. So I wrote this function:
remove_total <- function(data, x) {
data %>%
dplyr::filter(!stringr::str_detect(x, "Total$"))
}
Then when I try to run it, the following appears:
remove_total(D3, institution)
! Problem with filter() input ..1.
i Input ..1 is !stringr::str_detect(x, "Total$").
x object 'institution' not found
When I run rlang::last_trace(), I get this:
! Problem with filter() input ..1.
i Input ..1 is !stringr::str_detect(x, "Total$").
x object 'institution' not found
Backtrace:
- global remove_total(D3, institution)
- base::.handleSimpleError(...)
- dplyr h(simpleError(msg, call))
What am I doing wrong?
Thank you