简体   繁体   English

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

[英]Running Mann-Kendall on multiple columns in r

I am new in R and would like to run a mann-kendall on multiple columns at once.我是 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"
))

This is a sample of my data这是我的数据示例

Here is what I tried for an individual column这是我为单个列尝试的内容

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

I am hoping the get a table with the values of tau and p-value for all columns.我希望得到一个表,其中包含所有列的 tau 和 p 值。

We can use lapply to loop over the columns of interest.我们可以使用lapply来遍历感兴趣的列。 Here, the first column is dropped as it is 'Year'在这里,第一列被删除,因为它是“年”

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

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

Or with dplyr或者用dplyr

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

If we want as a table如果我们想当一张桌子

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    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM