Dealing with tibbles?
Closed this issue · 3 comments
I ran into the following issue when working on eatRecode
:
recodeList_2 <- readxl::read_xlsx(here::here("tests", "testthat", "helper_recodeDB_2.xlsx"))
recoded_df2 <- useRecodeList(df, varName = "country", new_varName = "country_r",
recodeList = recodeList_2)
gave the error message:
Fehler in eatTools::recodeLookup(df[, varName], lookup = recodeList) :
Old values in lookup table are not unique.
The useRecodeList()
function uses recodeLookup
internally.
When loading the data like this it works just fine:
recodeList_2 <- as.data.frame(readxl::read_xlsx(here::here("tests", "testthat", "helper_recodeDB_2.xlsx")))
I pinned the error down to this line in recodeLookup()
:
if (length(unique(lookup[, 1])) < nrow(lookup)) {
This extracts a column in a tibble: my_tibble[, 1]
... and a vector in a data.frame: my_data.frame[, 1]
So in the first case the length is 1, and in the second 6 (in my example).
One fix might be using the double brackets in this case:
if (length(unique(lookup[[1])) < nrow(lookup)) {
But let me know what you think!
Thanks for your tip! However, I think length(unique(lookup[[1]))
might fail if lookup
is given as a matrix. I think I would replace the first line of the function
if(!is.data.frame(lookup) && !is.matrix(lookup)) {stop("The lookup table must be a data.frame or matrix.")}
with
lookup <- makeDataFrame(lookup)
Is there a reason this is still open? I think this was fixed in commit 77dbab7?
Right. Issue can be closed.