简体   繁体   中英

How to put/save all elements of a List into one Excel sheet in R?

I have a list (bbb) with 5 elements in it, ie, each element for a year, like 2010, 2011, ... , 2014:

The first one in the list is this:

> bbb[1]
$`2010`
              Date      Average
X2010.01.01 2010-01-01 2.079090e-03
X2010.01.02 2010-01-02 5.147627e-04
X2010.01.03 2010-01-03 2.997464e-04
X2010.01.04 2010-01-04 1.375538e-04
X2010.01.05 2010-01-05 1.332109e-04

The second one in the list is this:

> bbb[2]
$`2011`
              Date      Average
X2011.01.01 2011-01-01 1.546253e-03
X2011.01.02 2011-01-02 1.152864e-03
X2011.01.03 2011-01-03 1.752446e-03
X2011.01.04 2011-01-04 2.639658e-03
X2011.01.05 2011-01-05 5.231150e-03
X2011.01.06 2011-01-06 8.909878e-04

And so on.

Here is my question:

How can I save all of these list's elements in 1 sheet of an Excel file to have something like this:

在此处输入图像描述

Your help would be highly appreciated.

You can do this using dcast .

bbb <- list(`2010` = data.frame(date = as.Date("2010-01-01") + 0:4,
                                avg = 1:5),
            `2011` = data.frame(date = as.Date("2011-01-01") + 0:5,
                                avg = 11:16),
            `2012` = data.frame(date = as.Date("2012-01-01") + 0:9,
                                avg = 21:30),
            `2013` = data.frame(date = as.Date("2013-01-01") + 0:7,
                                avg = 21:28))
df <- do.call("rbind", bbb)
df$year <- format(df$date, format = "%Y")
df$month_date <- format(df$date, format = "%b-%d")

library(data.table)
library(openxlsx)
df_dcast <- dcast(df, month_date~year, value.var = "avg") 
write.xlsx(df_dcast, "example1.xlsx")

Or using spread

library(dplyr)
library(tidyr)
df2 <- df %>% 
  select(-date) %>%
  spread(key = year, value = avg)
write.xlsx(df2, "example2.xlsx")

This isn't very pretty, but it's the best I could think of right now. But you could take the dataframes and loop through the list, joining them by date like this:

library(tidyverse)
library(lubridate)

bbb <-  list(`2010` = tibble(date = c('01-01-2010', '01-02-2010', '01-03-2010', '01-04-2010', '01-05-2010'),
                             average = 11:15),
              `2011` = tibble(date = c('01-01-2011', '01-02-2011', '01-03-2011', '01-04-2011', '01-05-2011'),
                             average = 1:5),
             `2012` = tibble(date = c('01-01-2012', '01-02-2012', '01-03-2012', '01-04-2012', '01-05-2012'),
                             average = 6:10))

for (i in seq_along(bbb)) {
  if(i == 1){
    df <- bbb[[i]] %>% 
      mutate(
        date = paste(day(as.Date(date, format = '%m-%d-%Y')), 
                     month(as.Date(date, format = '%m-%d-%Y'), label = TRUE), 
                     sep = '-')
      )
    colnames(df) <- c('date', names(bbb[i])) # Assuming your list of dataframes has just 2 columns: date and average
  } else {
    join_df <- bbb[[i]] %>% 
      mutate(
        date = paste(day(as.Date(date, format = '%m-%d-%Y')), 
                     month(as.Date(date, format = '%m-%d-%Y'), label = TRUE), 
                     sep = '-')
      )
    colnames(join_df) <- c('date', names(bbb[i]))
    df <- full_join(df, join_df, by = 'date')
  }
}

This loops through the list of dataframes and reformats the dates to Day-Month.

# A tibble: 5 x 4
  date  `2010` `2011` `2012`
  <chr>  <int>  <int>  <int>
1 1-Jan     11      1      6
2 2-Jan     12      2      7
3 3-Jan     13      3      8
4 4-Jan     14      4      9
5 5-Jan     15      5     10

You could then write that out with the writexl package function write_xlsx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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