0

I am importing roughly 140 large datasets. I am trying to build a function that imports these datasets and renames them with a QQYYYY (quarter, year) identifier. The (simplified) code I have thus far is below:

orig.first <- 'hist.dta_'
file.type <- '.txt'

clean.data <- function(qqyyyy) {
data.file <- paste(orig.first,qqyyyy,file.type,sep="")
origfile_ <<- read.table(data.file, sep="|", header=FALSE, 
colClasses=origclass, quote="")
}

Currently this imports the dataset "hist.dta_Q11999.txt" globally fine, however, it will import it with the name "origfile_". I would like to concatenate, say, Q11999 (to represent the first quarter of 1999) onto origfile_ so it becomes "origfile_Q11999". Could somebody let me know a clean way to achieve this?

  • 2
    Hack-creating objects in the global namespace usually ends up leading to deep, dark paths you wish you'd never tread upon. Is there a reason you don't want to read them into a list or separate environment? – hrbrmstr Jan 23 '17 at 05:03
  • I'm not aware of what's at the end of those deep dark paths; how would you recommend doing this? I plan to concatenate the datasets together into one large time-series dataset for analysis. – Econometrics33 Jan 23 '17 at 05:12
  • 1
    So you do want to read them in to a list and then process the list, probably with `do.call(rbind, yourlistname)`. There are many answered question on SO demonstrating how to do this and both hrbrmstr and I are tired of answering them or searching for them, so we think you should make an effort at searching first. – IRTFM Jan 23 '17 at 05:37
  • There are almost countless posts on SO for iterating over files and binding into a giant data frame. There are two examples in here https://github.com/hrbrmstr/rstudioconf2017 #research – hrbrmstr Jan 23 '17 at 12:17
  • Read gregor's answer on [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for working with lists of data.frames. Read [this post](http://stackoverflow.com/questions/1727772/quickly-reading-very-large-tables-as-dataframes-in-r) for efficiently reading in files. – lmo Jan 23 '17 at 13:23

1 Answers1

0

As others already suggested, here a solution how to concatenate all to one dataframe:

quarters <- c("Q11999") # append here other quarters or generate somehow

origData <- lapply(quarters, function(qqyyyy) {
data.file <- paste(orig.first,qqyyyy,file.type,sep="")
origfile <<- read.table(data.file, sep="|", header=FALSE, colClasses=origclass, quote="")
origfile$quarter <- qqyyyy
return(origfile)
})
origData <- do.call(rbind, origData)
c0bra
  • 1,031
  • 5
  • 22