Here is an R function that merges sheets with common time column, either read in using openxlsx::read.xlsx or read.csv. Depending on the setting you can produce different output formats.
Additionally, if it happens you have identical names in the separate sheets, so mergeing gets tricky, we can provide new names to rename them.
> rd_merge <- \(r, new_names, tcol=1, long=FALSE) {
+ yr <- el(lapply(r, names))[tcol]
+ ncv <- el(lapply(r, ncol)) - 1L
+ if (!missing(new_names)) {
+ r <- Map(setNames, r,
+ Map('c', yr, lapply(new_names, paste,
+ if (ncv > 1) seq_len(ncv) else '', sep=''))
+ )
+ }
+ if (long) {
+ r <- Map(`[<-`, r, '.id', value=seq_along(r))
+ } else {
+ stopifnot(Reduce(identical, sapply(r, names)[1, ]))
+ }
+ Reduce(\(...) merge(..., all=TRUE), x=r)
+ }
Usage
Excel data as shown in OP
> rd_ex <- Map(openxlsx::read.xlsx, 'foo.xlsx', sheet=1:2) ## recommended
> rd_merge(rd_ex)
Year A B st1 st2
1 2000 0.2 0.3 blue blue
2 2001 0.5 0.4 red red
3 2002 0.1 0.2 yellow yellow
4 2003 0.2 0.3 green green
5 2004 0.5 0.1 white white
6 2005 0.2 0.2 black black
> rd_merge(rd_ex, new_names=c('foo', 'bar')) ## same, but using new names
Year foo1 foo2 bar1 bar2
1 2000 0.2 0.3 blue blue
2 2001 0.5 0.4 red red
3 2002 0.1 0.2 yellow yellow
4 2003 0.2 0.3 green green
5 2004 0.5 0.1 white white
6 2005 0.2 0.2 black black
> rd_merge(rd_ex, long=TRUE, new_names='x') ## desired (?) long format
Year x1 x2 .id
1 2000 0.2 0.3 1
2 2000 blue blue 2
3 2001 0.5 0.4 1
4 2001 red red 2
5 2002 0.1 0.2 1
6 2002 yellow yellow 2
7 2003 0.2 0.3 1
8 2003 green green 2
9 2004 0.5 0.1 1
10 2004 white white 2
11 2005 0.2 0.2 1
12 2005 black black 2
Here an example with sheets that have identical names,
> rd_ex <- Map(openxlsx::read.xlsx, 'foo2.xlsx', sheet=1:2)
> lapply(rd_ex, names)
$`foo2.xlsx`
[1] "Year" "st1"
$<NA>
[1] "Year" "st1"
that we can't merge that easily. But with the function we can provide new names.
> rd_merge(rd_ex, new_names=c('foo', 'bar'))
Year foo bar
1 2000 0.2 blue
2 2001 0.5 red
3 2002 0.1 yellow
4 2003 0.2 green
5 2004 0.5 white
6 2005 0.2 black
We can do the same with your read in .csv files.
> rd_csv <- lapply(c('foo21.csv', 'foo21.csv'), read.csv)
> lapply(rd_csv, names)
[[1]]
[1] "Year" "st1"
[[2]]
[1] "Year" "st1"
> rd_merge(rd_csv, new_names=c('temperature', 'ppt'))
Year temperature ppt
1 2000 0.2 blue
2 2001 0.5 red
3 2002 0.1 yellow
4 2003 0.2 green
5 2004 0.5 white
6 2005 0.2 black