Search code examples
rstatisticsexport-to-excel

How do I export multiple values after using lapply?


I am using this code:

library(trend)

out <- lapply(Všetky_trendy_tau[-1], sens.slope)
out

It produces this ( there are multiple sections like this one, each for one of the columns, here is the column for SO2 levels between months 10-12)

`$`SO2 10-12`
Sen's slope
data:  X[[i]]
z = -3.4715, n = 20, p-value = 0.0005175
alternative hypothesis: true z is not equal to 0
95 percent confidence interval:
 -0.5890185 -0.2127360
sample estimates:
Sen's slope 
 -0.4057382`

I would like to export all of the Sens slope values into one table, so I dont have to copy them separately.

I have tried this:

print(out)
library("writexl")
data.frame("out")
write_xlsx(out,"C:/Users/natal/Rexport/BBQ.xlsx")

But I cant make a df from "out":

> data.frame("out")
  X.out.
1    out
> write_xlsx(out,"C:/Users/natal/Rexport/BBQ.xlsx")
Error in write_xlsx(out, "C:/Users/natal/Rexport/BBQ.xlsx") : 
  Argument x must be a data frame or list of data frames`

How do I go about exporting these values?

Running dput(Všetky_trendy_tau) resulted in

Všetky_trendy_tau <- structure(list(BB = c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 
2020, 2021), `Tmean 6-10` = c(15.61, 16.0296, 14.8558, 15.1362, 
16.0752, 15.4374, 15.6108, 15.945, 15.1134, 15.948, 16.4392, 
16.3104, 15.8556, 16.8374, 15.8104, 16.079, 17.0034, 16.6396, 
16.1254, 16.1732), `Tmin 10-12` = c(-7.66666666666667, -9.46666666666667, 
-5.73333333333333, -7.33333333333333, -7.7, -8.5, -6.33333333333333, 
-7.33333333333333, -8.2, -7.16666666666667, -5.9, -6.36666666666667, 
-6.2, -6.36666666666667, -6.4, -6.43333333333333, -6.4, -5.2, 
-3.93333333333333, -5.8), `Tmax 6-12` = c(23.3714285714286, 24.7, 
22.3285714285714, 23.0714285714286, 23.6142857142857, 23.0857142857143, 
23.8857142857143, 23.9857142857143, 22.6571428571429, 24.7142857142857, 
24.8857142857143, 24.6857142857143, 25.0571428571429, 26.3285714285714, 
23.6714285714286, 24.6, 24.9285714285714, 25.0285714285714, 23.8, 
23.9142857142857), `P 8-11` = c(100.45, 46.125, 77.6, 58.9, 50.15, 
56.5, 53.275, 73.5, 123.925, 17.05, 81.425, 70.65, 81.15, 86.05, 
81.75, 91.85, 56.675, 96.35, 110.05, 58.5), `RH 10-12` = c(84.811, 
80.918, 80.1116666666667, 79.8046666666667, 79.3496666666667, 
78.1223333333333, 80.1686666666667, 82.0523333333334, 80.9496666666667, 
81.3843333333333, 82.453, 79.8876666666667, 83.758, 86.4053333333333, 
83.0646666666667, 84.711, 81.5076666666667, 83.8046666666667, 
89.0506666666667, 86.2046666666667), `CO 3-5` = c(146.252536487455, 
205.331192244279, 154.677120059325, 138.623544659498, 245.956376021505, 
204.970181182796, 315.741054444444, 352.060383317403, 557.612969964158, 
464.153823694556, 399.822623809069, 465.976030548705, 388.233706797462, 
352.646727819846, 317.059748842229, 389.236876134842, 388.10403925264, 
335.72588316185, 672.143869799177, 630.511507522429), `CO 6-9` = c(94.6906316258806, 
139.369308655914, 110.019195955335, 77.5906647311828, 178.666819624878, 
175.215383091398, 184.621740967742, 412.632739827586, 462.273514110835, 
340.134667783429, 314.304749591985, 303.08009905403, 302.143152860665, 
305.570477471275, 309.203320997342, 279.585374556588, 255.045736577133, 
295.292933982, 574.300989394008, 454.302910062475), `SO2 4` = c(12.8758620689655, 
9.69395666666666, 9.12915, 7.92364666666667, 9.24036333333334, 
9.45277666666667, 6.81799, 5.68459333333333, 3.81932666666667, 
4.91594823529412, 6.73355861386138, 3.07619351351351, 5.16612718168813, 
6.0317522959901, 6.89737741029208, 4.5505486908078, 4.00705627615062, 
6.61856664161849, 7.84178872992701, 6.73666156458636), `SO2 10-12` = c(16.9684951612903, 
11.2210772401434, 10.6016353046595, 11.3203097491039, 9.32796505942275, 
8.38681598290598, 9.11243014336918, 6.04923831541219, 6.22740977092099, 
7.41129373021993, 7.13273708591053, 5.36504383637292, 6.70726499471553, 
5.28382236789851, 3.86037974108148, 2.97481760852231, 8.2800826033021, 
8.76253810466923, 5.30336200388185, 6.25562986634609)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

Solution

  • Here's a way with base R:

    out <- t(as.data.frame(lapply(Vsetky_trendy_tau[,-1], \(x) sens.slope(x)$estimates)))
    

    Here's a way with tidyverse:

    map_dfc(Vsetky_trendy_tau[,-1], ~ sens.slope(.x)$estimates) %>% 
      pivot_longer(everything())
    
    # A tibble: 9 × 2
      name         value
      <chr>        <dbl>
    1 Tmean 6-10  0.0602
    2 Tmin 10-12  0.124 
    3 Tmax 6-12   0.0908
    4 P 8-11      2.05  
    5 RH 10-12    0.331 
    6 CO 3-5     18.2   
    7 CO 6-9     14.9   
    8 SO2 4      -0.235 
    9 SO2 10-12  -0.406 
    

    Exporting it:

    library(openxlsx)
    
    write.xlsx(out, "results.xlsx")