Under a directory, I have multiple excel files with similar format (you may download sample files from here):
I will need to
- loop files and
read_excel()
, - mutate a new column
name
with second column name, - rename the first and the second column to
date
andvalue
respectively, remove last column (whose original column name is1
); - append all dfs to one dataframe using
do.call(rbind, df.list)
What I have done:
To loop and get files paths:
library(fs)
folder_path <- './test/'
file_paths <- dir_ls(folder_path, regexp = ".xlsx")
Function to read excels:
read_excel_file <- function(path) {
df <- read_excel(path = path, header = TRUE)
}
lapply read_excel()
function to each excel file:
df.list = lapply(file_paths, function(file) read_excel(file, skip = 2, col_names = FALSE))
df <- do.call(rbind, df.list)
The expected result will be a dataframe like this:
date value name
2 2021-01-07 -76.5 J05-J01
3 2021-01-08 -93.5 J05-J01
4 2021-01-15 -305 J05-J01
5 2021-01-22 289 J05-J01
6 2021-01-29 242.5 J05-J01
7 2021-02-05 266 J05-J01
8 2021-02-10 239.5 J05-J01
9 2021-02-19 305.5 J05-J01
10 2021-01-07 323 J01-J09
11 2021-01-08 317.5 J01-J09
12 2021-01-15 527.5 J01-J09
13 2021-01-22 -51 J01-J09
14 2021-01-29 -58.5 J01-J09
15 2021-02-05 -76 J01-J09
16 2021-01-07 76.5 J01-J05
17 2021-01-08 93.5 J01-J05
18 2021-01-15 305 J01-J05
19 2021-01-22 -289 J01-J05
20 2021-01-29 -242.5 J01-J05
21 2021-02-05 -266 J01-J05
22 2021-02-10 -239.5 J01-J05
How could I achieve that using R? Thanks a lot at advance.