merge excel sheets into data.frame

--Hello,

i try to merge excel sheets from a workbook into a dataframe, this is the code i use:

library(dplyr)
library("readxl")
library("purrr")

k <- map(1:4,    # 4 sheets with the same first column 'targets'
 ~ read_excel("workbook.xlsx",
              sheet = .x)) %>%
         reduce(left_join, by  = "targets")

df <- as.data.frame(k)

i works fine, but the column headers don't match the sheet names. How can I do this automatically?
i know i can do it manually like that:

sfx <- c("targets", "df1", "df2")
setnames(df,sfx)

thank you --

2 Likes

The article below seems to address a similar issue to yours. You may find it helpful.
https://www.r-bloggers.com/2022/06/robservations-33-merging-excel-spreadsheets-with-base-r-and-openxlsx/

1 Like