Home > Software engineering >  R - How to create a panel data set from multiple spreasheets of data?
R - How to create a panel data set from multiple spreasheets of data?

Time:11-19

I have data in separate excel sheets as follows:

YEAR_1.xlsx Name | Height | Age .... | ...... | ...

YEAR_2.xlsx Name | Height | Age .... | ...... | ...

Is there a way to get R to make a panel data set out of these separate sheets?

Thanks in advance

While looking up the answer to this I've found out that my data is unusually structured (I guess). I have first looked into merging, but that does something else entirely. I have found something with the plm package, but since I know pretty much nothing about R it wasn't much help for me

CodePudding user response:

The panel dataset does not have a direct definition in base R so we could interpret it as a simple data.frame which bind each xlsx sheet. On the other hand there are separate packages like plm which have a direct definition and methods for such.

# Example sets - you already have them
years <- 1:10

for (year in years) {
  aa <- airquality
  aa$year <- year
  xlsx::write.xlsx(aa, sprintf("year%s.xlsx", year), row.names = FALSE)
}

# How to create panel

sets <- list()
years <- 1:10
for (year in years) {
  sets[[year]] <- xlsx::read.xlsx(sprintf("year%s.xlsx", year), sheetIndex = 1)
}

panel <- do.call(rbind, sets)

# plm panel - plm package has to be isntalled
# install.packages("plm")
panel_plm <- plm::pdata.frame(panel, "year")

CodePudding user response:

Figured it out, I just used rbind to put the second table "behind" the first one and then used plm.

  • Related