I have the following data frame called result
.
MANUAL.ID | AUTO.ID | loc |
---|---|---|
NA | PYPPYP | L2 |
PIPpip | NA | L1 |
Barbar | NA | L5 |
NA | Pippip | L3 |
NA | Pippip,BerBar | L3 |
I try to replace all the different forms of a same tag by the right one. For example replace all PIPPIP and PIPpip by Pippip or Berbar by Barbar. To do this, I use a mutate
function with case_when
based on a required file called tesaurus
which have column with all the possible case of a same tag (tag_id) and a column with the correct one (tag_ok) which looks like this :
tag_id | tag_ok |
---|---|
PYPPYP | Pippip |
PIPpip | Pippip |
Pippip | Pippip |
Barbar | Barbar |
I use the following code.
library("plyr")
library("dplyr")
library("reshape")
library("data.table")
library("stringr")
library("tidyr")
library("openxlsx")
tesaurus <- read.xlsx("Requested_files/sp_tesaurus.xlsx", sheet = "tesaurus") %>%
select(-bat_sp)
result <- result %>%
mutate_at(
vars(contains("ID")),
list(as.formula(
paste0(
"~ case_when(",
paste(
sprintf(
"str_detect(., pattern = regex('%s', ignore_case = TRUE)) ~ '%s'",
tesaurus$tag_id,
tesaurus$tag_ok
),
collapse = ", "
), ", TRUE ~ .)"
)
))
)
My problem is that when I run it I have this error message :
Error in `mutate()`:
ℹ In argument: `AUTO.ID = (structure(function (..., .x = ..1, .y = ..2, . = ..1) ...`.
Caused by error in `case_when()`:
! Can't combine `..1 (right)` <character> and `..161 (right)` <double>.
---
Backtrace:
▆
1. ├─result %>% ...
2. ├─dplyr::mutate_at(...)
3. │ ├─dplyr::mutate(.tbl, !!!funs)
4. │ └─dplyr:::mutate.data.frame(.tbl, !!!funs)
5. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
6. │ ├─base::withCallingHandlers(...)
7. │ └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
8. │ └─mask$eval_all_mutate(quo)
9. │ └─dplyr (local) eval()
10. ├─`<inln_cl_>`(AUTO.ID)
11. │ └─rlang::eval_bare(`_quo`, base::parent.frame())
12. ├─rlang (local) case_when(...)
13. └─dplyr::case_when(...)
So, what is the problem and how can I fix it ?