I am compiling a report and for some statistical analyses, I am using the z-scores for certain columns in my dataframe. This required me to use the scale function, but recently, I have been tessting out using datawizard instead, where there is a function called datawizard::standardize to accomplish the exact same task.
However, later on in my report, I was showing some plots of the distribution on my data and had been using pivot_longer to show multiple variables in the same graph. It worked when i had the scale function originally, but now I'm struggling with using datawizard::standardize.
My code for the graph is below. I now get an error in pivot_longer_spec()
. A screenshot of the full error is below.
! Can't combine variable1.z
<dw_transformer> and variable2.z
<dw_transformer>.
✖ Some attributes are incompatible.
df %>%
filter(index.visit == visit) %>%
select(ends_with(".z")) %>%
pivot_longer(cols = everything()) %>%
ggplot(aes(x = name, y = value)) +
geom_quasirandom(size = .1)
The thing is, I tried switching to specifying only one biomarker instead of everything in my code and that works, but it seems impossible for me to get more than that. I'd like to show all the data side-by-side in one graph.
You can solve the problem by wrapping the standardize()
transformation in c()
, which essentially changes the class from dw_transform
to numeric
. Here's an example with mtcars
. First, you can generate the error this way:
library(dplyr)
library(datawizard)
library(tidyr)
mtcars %>%
as_tibble(rownames="model") %>%
select(model, hp, mpg) %>%
mutate(across(c(hp, mpg), function(x)standardize(x))) %>%
pivot_longer(-model)
#> Error in `pivot_longer()`:
#> ! Can't combine `hp` <dw_transformer> and `mpg` <dw_transformer>.
#> ✖ Some attributes are incompatible.
#> ℹ The author of the class should implement vctrs methods.
#> ℹ See <https://vctrs.r-lib.org/reference/faq-error-incompatible-attributes.html>.
#> Backtrace:
#> ▆
#> 1. ├─... %>% pivot_longer(-model)
#> 2. ├─tidyr::pivot_longer(., -model)
#> 3. ├─tidyr:::pivot_longer.data.frame(., -model)
#> 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 = call)
Here's the code that solves the error by wrapping the output of standardize()
in c()
.
mtcars %>%
as_tibble(rownames="model") %>%
select(model, hp, mpg) %>%
mutate(across(c(hp, mpg), function(x)c(standardize(x)))) %>%
pivot_longer(-model)
#> # A tibble: 64 × 3
#> model name value
#> <chr> <chr> <dbl>
#> 1 Mazda RX4 hp -0.535
#> 2 Mazda RX4 mpg 0.151
#> 3 Mazda RX4 Wag hp -0.535
#> 4 Mazda RX4 Wag mpg 0.151
#> 5 Datsun 710 hp -0.783
#> 6 Datsun 710 mpg 0.450
#> 7 Hornet 4 Drive hp -0.535
#> 8 Hornet 4 Drive mpg 0.217
#> 9 Hornet Sportabout hp 0.413
#> 10 Hornet Sportabout mpg -0.231
#> # ℹ 54 more rows
Created on 2023-09-07 with reprex v2.0.2