【发布时间】:2020-11-19 23:54:22
【问题描述】:
我正在尝试将 dplyr 的 summarise(across()) 与 MannKendall 函数一起使用(返回一个列表)。如果我不使用 group_by() ,使用它没有问题,但是当我使用它时会出现错误。这是我的代码:
Preds %>% group_by(Municipality) %>%
summarise(across(where(is.numeric),~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
错误:无法组合 Municipality 和 Yield 。
产量是我的变量之一,它是数字
这可行,但不是我需要的:
Preds %>%
summarise(across(where(is.numeric),~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
举个可重现的例子:
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), ~list(MannKendall(.) %>%tidy %>% select(p.value, statistic)))) %>%
pivot_longer(everything()) %>%
unnest(c(value))
我认为问题在于尝试整理每列中包含列表的结果数据框
【问题讨论】:
-
请展示一个可重现的小例子
-
如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。
-
谢谢!我意识到问题出在整洁的部分。这有效 Preds %>% group_by(Municipality) %>% summarise(across(where(is.numeric),~list(MannKendall(.))) 所以这只是“整理”结果数据帧的问题,其中所有列包含列表。