Closed siavash-babaei closed 1 year ago
Thanks for posting this - this is a new error introduced in the latest release, I confirm that I get the same error:
# Library for dealing with missing values
library(naniar)
# Load `oceanbuoys` data
data("oceanbuoys")
# Impute the mean value and track the imputations
ocean_imp_mean <- oceanbuoys |>
naniar::nabular(only_miss = TRUE) |>
naniar::impute_mean_all() |>
naniar::add_label_shadow()
# Gather the imputed data: Throws an error
ocean_imp_mean |>
naniar::shadow_long(humidity, air_temp_c)
#> Error in `tidyr::pivot_longer()`:
#> ! Can't combine `year` <double> and `any_missing` <character>.
#> Backtrace:
#> ▆
#> 1. ├─naniar::shadow_long(ocean_imp_mean, humidity, air_temp_c)
#> 2. │ ├─tidyr::pivot_longer(...)
#> 3. │ └─tidyr:::pivot_longer.data.frame(...)
#> 4. │ └─tidyr::pivot_longer_spec(...)
#> 5. │ └─vctrs::vec_ptype_common(...)
#> 6. └─vctrs (local) `<fn>`()
#> 7. └─vctrs::vec_default_ptype2(...)
#> 8. ├─base::withRestarts(...)
#> 9. │ └─base (local) withOneRestart(expr, restarts[[1L]])
#> 10. │ └─base (local) doWithOneRestart(return(expr), restart)
#> 11. └─vctrs::stop_incompatible_type(...)
#> 12. └─vctrs:::stop_incompatible(...)
#> 13. └─vctrs:::stop_vctrs(...)
#> 14. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))
Created on 2023-03-30 with reprex v2.0.2
Thanks again for posting this, this will be fixed in an upcoming release.
Thank you again for this @siavash-babaei !
This now works, but by default changes value
to character, as that is the safest way to have this always succeed. Otherwise you can specify your own coercion function to transform value
values. Here's an example:
library(naniar)
aq_shadow <- nabular(airquality)
shadow_long(aq_shadow)
#> # A tibble: 918 × 4
#> variable value variable_NA value_NA
#> <chr> <chr> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Wind 7.4 Wind_NA !NA
#> 4 Temp 67 Temp_NA !NA
#> 5 Month 5 Month_NA !NA
#> 6 Day 1 Day_NA !NA
#> 7 Ozone 36 Ozone_NA !NA
#> 8 Solar.R 118 Solar.R_NA !NA
#> 9 Wind 8 Wind_NA !NA
#> 10 Temp 72 Temp_NA !NA
#> # ℹ 908 more rows
# then filter only on Ozone and Solar.R
shadow_long(aq_shadow, Ozone, Solar.R)
#> # A tibble: 306 × 4
#> variable value variable_NA value_NA
#> <chr> <chr> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Ozone 36 Ozone_NA !NA
#> 4 Solar.R 118 Solar.R_NA !NA
#> 5 Ozone 12 Ozone_NA !NA
#> 6 Solar.R 149 Solar.R_NA !NA
#> 7 Ozone 18 Ozone_NA !NA
#> 8 Solar.R 313 Solar.R_NA !NA
#> 9 Ozone <NA> Ozone_NA NA
#> 10 Solar.R <NA> Solar.R_NA NA
#> # ℹ 296 more rows
# ensure `value` is numeric
shadow_long(aq_shadow, fn_value_transform = as.numeric)
#> # A tibble: 918 × 4
#> variable value variable_NA value_NA
#> <chr> <dbl> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Wind 7.4 Wind_NA !NA
#> 4 Temp 67 Temp_NA !NA
#> 5 Month 5 Month_NA !NA
#> 6 Day 1 Day_NA !NA
#> 7 Ozone 36 Ozone_NA !NA
#> 8 Solar.R 118 Solar.R_NA !NA
#> 9 Wind 8 Wind_NA !NA
#> 10 Temp 72 Temp_NA !NA
#> # ℹ 908 more rows
shadow_long(aq_shadow, Ozone, Solar.R, fn_value_transform = as.numeric)
#> # A tibble: 306 × 4
#> variable value variable_NA value_NA
#> <chr> <dbl> <chr> <fct>
#> 1 Ozone 41 Ozone_NA !NA
#> 2 Solar.R 190 Solar.R_NA !NA
#> 3 Ozone 36 Ozone_NA !NA
#> 4 Solar.R 118 Solar.R_NA !NA
#> 5 Ozone 12 Ozone_NA !NA
#> 6 Solar.R 149 Solar.R_NA !NA
#> 7 Ozone 18 Ozone_NA !NA
#> 8 Solar.R 313 Solar.R_NA !NA
#> 9 Ozone NA Ozone_NA NA
#> 10 Solar.R NA Solar.R_NA NA
#> # ℹ 296 more rows
Created on 2023-05-01 with reprex v2.0.2
shadow_long
throws an error when gathering variables:pivot_longer
cannot combine variables!!!