Daphine
Daphine

Reputation: 1

how do I merge multiple xlsx sheets in r

run the code below in attempt to merge 3 sheets

Data_v01 <- merge(
              Data, 
              crop_income_cal,
              livestock_income_cal, 
              by = "parent_index_new", 
              all.x = TRUE, 
              all.y = TRUE)

but got an error

Error in fix.by(by.x, x) : 'by' must specify one or more columns as numbers, names or logical

Upvotes: 0

Views: 254

Answers (2)

Merijn van Tilborg
Merijn van Tilborg

Reputation: 5887

You can use Reduce to merge a list of multiple data.frames

Reduce(f = function(x, y) merge(x, y, by = c("parent_index_new"), all = T), list(Data, crop_income_cal, livestock_income_cal))

Upvotes: 0

Reda
Reda

Reputation: 497

You can use tidyverse and readxl libraries

library(tidyverse)
library(readxl)

#See your excel sheets

excel_sheets("path/to/your/file/file.xlsx")
#Combine all your sheets 
excel_sheets("path/to/your/file/file.xlsx") %>% map_df(~read_xlsx("path/to/your/file/file.xlsx",.))

you can also use

excel_sheets("path/to/your/file/file.xlsx") %>% map(~read_xlsx("path/to/your/file/file.xlsx",.))

#Put the result in a object 

excel_sheets("path/to/your/file/file.xlsx") %>% map(~read_xlsx("path/to/your/file/file.xlsx",.)) -> my_data

Upvotes: 1

Related Questions