【问题标题】:How to create dataframe using results (output) of sens.slope function?如何使用 sens.slope 函数的结果(输出)创建数据框?
【发布时间】:2020-05-03 10:08:49
【问题描述】:

我有一个包含多张工作表的 Excel 数据。我将它们导入 R 并使用函数 sens.slope() 应用 Mann-Kendall 趋势测试。此函数的结果在 htest 类中,但我想将它们放在一个表格中。

我安装了所需的包并导入了每张数据集。

require(readxl)
require(trend)
tmin1 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "acipayam")
tmin2 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "adana")
...
tmin57 <- read_excel("C:/TEZ/ANALİZ/future_projection/2051-2100/model 3-3/average_tmin_3_3_end.xlsx", sheet = "yumurtalik")

然后,指定趋势测试的列。

x1<-tmin1$`13`
x2<-tmin1$`14`
x3<-tmin1$`15`
x4<-tmin1$`16`
x5<-tmin1$`17`
...
x281<-tmin57$`13`
x282<-tmin57$`14`
x283<-tmin57$`15`
x284<-tmin57$`16`
x285<-tmin57$`17`

并应用了函数。

sens.slope(x1)
sens.slope(x2)
sens.slope(x3)
....
sens.slope(x285)

结果是这样的。

> sens.slope(x1)

    Sen's slope

data:  x1
z = 4.6116, n = 49, p-value = 3.996e-06
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
 0.03241168 0.08101651
sample estimates:
Sen's slope 
 0.05689083 

> sens.slope(x2)

    Sen's slope

data:  x2
z = 6.8011, n = 49, p-value = 1.039e-11
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
 0.05632911 0.08373755
sample estimates:
Sen's slope 
 0.07032428 
...

如何将这些值放在一个表中并将它们写入 Excel 文件? (所需值的名称是函数中的 statisticestimates。)

【问题讨论】:

    标签: r excel dataframe trend


    【解决方案1】:

    有一个 package broom 正是为此:

    library(tidyverse)
    library(trend)
    
    sens.slope(runif(1000)) %>%
      broom::tidy()
    
    
    # A tibble: 1 x 7
      statistic p.value parameter   conf.low conf.high method      alternative
          <dbl>   <dbl>     <int>      <dbl>     <dbl> <chr>       <chr>      
    1     0.548   0.584      1000 -0.0000442 0.0000801 Sen's slope two.sided  
    

    如果您有很多数据框,请将它们全部绑定到一个列表中并用map_df 循环遍历:

    A = tibble(Value = runif(1000))
    B = tibble(Value = runif(1000))
    C = tibble(Value = runif(1000))
    D = tibble(Value = runif(1000))
    
    list(A,B,C,D) %>%
      map_df(~.x %>% 
               pull(1) %>%
               sens.slope() %>%
               broom::tidy())
    
    # A tibble: 4 x 7
      statistic p.value parameter   conf.low  conf.high method      alternative
          <dbl>   <dbl>     <int>      <dbl>      <dbl> <chr>       <chr>      
    1    -0.376  0.707       1000 -0.0000732  0.0000502 Sen's slope two.sided  
    2    -2.30   0.0215      1000 -0.000138  -0.0000110 Sen's slope two.sided  
    3    -1.30   0.194       1000 -0.000104   0.0000209 Sen's slope two.sided  
    4     0.674  0.500       1000 -0.0000410  0.0000848 Sen's slope two.sided
    

    编辑:刚刚意识到broom::tidy在这种情况下不提供估计(以前没有遇到过),这是不使用broom的解决方案:

    A = tibble(Value = runif(1000))
    B = tibble(Value = runif(1000))
    C = tibble(Value = runif(1000))
    D = tibble(Value = runif(1000))
    
    list(A,B,C,D) %>%
      purrr::map_df(.,~{
        Test = sens.slope(.x %>% pull(1))
        Test = tibble(Estimate = Test["estimates"] %>% unlist,
               Statistic = Test["statistic"] %>% unlist)
      }
     )
    
    # A tibble: 4 x 2
         Estimate Statistic
            <dbl>     <dbl>
    1 -0.0000495     -1.55 
    2 -0.00000491    -0.155
    3  0.0000242      0.755
    4 -0.0000301     -0.921
    

    【讨论】:

    • purrr 包没用,不过还是谢谢你的回答和关注。
    【解决方案2】:

    尝试使用列表而不是在全局环境中拥有这么多对象。

    现在既然您已经拥有它们,您可以将它们组合在一个列表中,对每个应用 sens.slope,从中提取 statisticestimates 并获取数据框。

    library(trend)
    
    output <- data.frame(t(sapply(mget(paste0('x', 1:285)), function(y) 
                         {temp <- sens.slope(y);c(temp$statistic, temp$estimates)})))
    

    您现在可以使用 write.csv 将此数据帧写入 csv。

    write.csv(output, 'output.csv', row.names = FALSE)
    

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 1970-01-01
      • 2015-10-12
      • 2016-09-09
      • 2021-07-21
      • 1970-01-01
      • 2018-08-05
      • 2020-10-28
      • 2018-01-02
      相关资源
      最近更新 更多