【问题标题】:Running Mann-Kendall on multiple columns in r在 r 中的多个列上运行 Mann-Kendall
【发布时间】:2020-09-17 19:13:59
【问题描述】:

我是 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 值。

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以使用lapply 来遍历感兴趣的列。在这里,第一列被删除,因为它是 '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
    

    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    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-29
      • 2019-08-24
      • 1970-01-01
      • 2019-06-28
      • 2018-04-02
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多