在 r 中的多个列上运行 Mann-Kendall

标签 r

我是 R 新手,希望同时在多个列上运行 mann-kendall。

structure(list(Year = c(1997, 1999, 2001, 2002), pH = c(8, 8.4, 
  8.2375, 8.27333333333333), Colour = c(16, 50.5, 21, 17.9090909090909
  )), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))

这是我的数据示例

这是我对单个列所做的尝试

MannKendall(NoordAnnual$Colour)
# tau = -0.137, 2-sided pvalue =0.4173

我希望得到一个包含所有列的 tau 值和 p 值的表格。

最佳答案

我们可以使用lapply来循环感兴趣的列。在这里,第一列被删除,因为它是“年份”

library(Kendall)     
out <- lapply(NoordAnnual[-1], MannKendall)
out
#$pH
#tau = 0.333, 2-sided pvalue =0.7341

#$Colour
#tau = 0, 2-sided pvalue =1

或者使用dplyr

library(dplyr)
NoordAnnual %>%
       summarise(across(-1,  ~list(MannKendall(.))))

如果我们想要作为表格

library(tidyr)
library(broom)
NoordAnnual %>%
      summarise(across(-1,  ~list(MannKendall(.) %>%
               tidy %>%
               select(p.value, statistic)))) %>%
      pivot_longer(everything()) %>%
      unnest(c(value))
# A tibble: 2 x 3
#  name   p.value statistic
#  <chr>    <dbl>     <dbl>
#1 pH       0.734     0.333
#2 Colour   1         0    

关于在 r 中的多个列上运行 Mann-Kendall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63944816/

相关文章:

r - 对数据框的行进行排序

r - 如何在R中获得非正方形图?

r - 使用 RStudio : why reading . RProfile 编译 RMarkdown?

r - 混淆包 'litt' 的 'elog2cbs' 函数中的 "BTYDplus"值

r - R语言函数中的函数

r - 将 csv 文件导入 R 换行问题

r - ggplot2 中的梯度 alpha 以 0 为中心?

r - 获取 Dataframe 的元素数量等于 List - R 中的元素数量

r - 在向量 R 中的值分隔符处拆分元素

json - 如何用rjson强制将字符编码为数组?