如何使用sens.slope函数的结果(输出)创建数据框?

时间:2020-05-03 10:08:49

标签: r excel dataframe trend

我有一个包含多个工作表的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文件? (所需值的名称是函数中的 statistic estimates 。)

2 个答案:

答案 0 :(得分:2)

为此专门有一个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

答案 1 :(得分:0)

尝试使用列表,而不要在全局环境中使用太多对象。

现在,由于已经拥有它们,因此可以将它们组合在一个列表中,对每个应用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)