0

I am currently trying to vertically merge several datasets of survey data into one big dataset. The datasets are in a folder on my desktop and are in dta format. Please how do I do this?

  • `list.files()` and than loop over all files importing them with `lapply()` and `read_dta()` from the haven package? if you could provide any example code or data, we could help you more. – Stephan Apr 21 '20 at 13:17
  • Does this answer your question? [Using R to list all files with a specified extension](https://stackoverflow.com/questions/4876813/using-r-to-list-all-files-with-a-specified-extension) – stasiaks Apr 21 '20 at 13:45

1 Answers1

0

I think the data.table package makes collapsing data frames stored in a list very easy:

library(haven)
library(data.table)

d1 <- data.frame(x = rpois(10, 13))
d2 <- data.frame(x = rpois(10, 13))
write_dta(d1, "1993.dta")
write_dta(d2, "1994.dta")

# get the file names
files <- list.files(pattern = "\\.dta$")

# write a function that reads in the data, and transforms it into a data.table object
open.files <- function(x){ setDT(read_dta(x)) }

# read in the files in a list and name the list
d <- lapply(files, open.files)
names(d) <- files

# rbind all the data.frames
d <- rbindlist(d, idcol = "filename", use.names = T)
d
     filename  x
 1: 1993.dta 10
 2: 1993.dta 13
 3: 1993.dta 14
 4: 1993.dta 13
 5: 1993.dta 12
 6: 1993.dta 17
 7: 1993.dta 12
 8: 1993.dta 18
 9: 1993.dta 11
10: 1993.dta 13
11: 1994.dta 13
12: 1994.dta 12
13: 1994.dta 15
14: 1994.dta 11
15: 1994.dta 11
16: 1994.dta  7
17: 1994.dta 16
18: 1994.dta 10
19: 1994.dta 11
20: 1994.dta 15

Alternatively, have a look here: Convert a list of data frames into one data frame

desval
  • 2,345
  • 2
  • 16
  • 23