I'm not understanding something fundamental in using pivot_longer. Let me share my results. Here a table. the temperature metrics are all doubles.
device_id < 12°C < 16°C < 18°C < 21°C > 21°C
412222 0 0.355 0.645 0.000 0.000
C000037 0 0.000 0.048 0.539 0.412
P000000 0 0.000 0.105 0.620 0.275
W000043 0 0.000 0.057 0.562 0.380
W000069 0 0.359 0.641 0.000 0.000
W000075 0 0.000 0.036 0.521 0.443
And the outut from dput() which hopefully will allow you to rebuild.
structure(list(device_id = c("412222", "C000037", "P000000",
"W000043", "W000069", "W000075"), `< 12°C` = c(0, 0, 0, 0, 0,
0), `< 16°C` = c(0.355, 0, 0, 0, 0.359, 0), `< 18°C` = c(0.645,
0.048, 0.105, 0.057, 0.641, 0.036), `< 21°C` = c(0, 0.539, 0.62,
0.562, 0, 0.521), `> 21°C` = c(0, 0.412, 0.275, 0.38, 0, 0.443
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-6L))
The following code does not do what I expect:
temp <-
risk_by_device %>%
tidyr::pivot_longer(
cols = c(select(., contains("°C")), na.rm = TRUE),
names_to = "Score",
values_to = "percentage"
)
Error in `as_indices_impl()`:
! Must subset columns with a valid subscript vector.
x Subscript has the wrong type `tbl_df<
< 12°C: double
< 16°C: double
< 18°C: double
< 21°C: double
> 21°C: double
>`.
i It must be numeric or character.
Backtrace:
1. risk_by_device %>% ...
23. rlang::cnd_signal(x)
Error in as_indices_impl(x, vars, call = call, strict = strict) :
I have run very similar code on a daa set with strings and that work. It's not that pivot_longer doesn't work on doubles, is it?