Reputation: 13
I am trying to use dplyr's summarise(across()) with the MannKendall function (returns a list). I have no problem using it if I don't use group_by() but when I do I get an error. This is my code:
Preds %>% group_by(Municipality) %>%
summarise(across(where(is.numeric),~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
Error: Can't combine Municipality
and Yield
.
Yield is one of my variables and it is numeric
This works but is not what I need:
Preds %>%
summarise(across(where(is.numeric),~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
For a reproducible example:
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), ~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
I think the problem is in trying to tidy the resulting dataframe containing lists in each column
Upvotes: 1
Views: 352
Reputation: 21284
You can use unnest_longer()
:
library(tidyverse)
library(broom)
library(Kendall)
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), ~list(
MannKendall(.) %>%
tidy %>%
select(p.value, statistic)))) %>%
unnest_longer(Sepal.Length) %>%
unnest_longer(Sepal.Width)
# A tibble: 3 x 3
Species Sepal.Length$p.value $statistic Sepal.Width$p.value $statistic
<fct> <dbl> <dbl> <dbl> <dbl>
1 setosa 0.926 -0.0102 0.920 -0.0111
2 versicolor 0.0753 -0.178 0.400 -0.0859
3 virginica 0.750 -0.0326 0.226 0.124
Upvotes: 2