0

Hello I am trying to use a script in R to open a large number of txt files downloaded from the EDGAR database. The files are doenloaded in mydocuments and are organized in subfolders by the CIK number of each firm and year. Inside each subfolder there are a number of txt files with names like this cik_8-K-year_month_date.txt. I am trying to use a loop in order to open each file and then use the TM package to get the terms I am interested in. The code I am trying to use is the following:

for (i in 1:dim(CIK)) {
  for(year in 1980:2017) {
    for (m in 1:12) {
      for (d in 1:30) {
        if(is.na(cik[i])) {
        } else {
          mydata <- read_file("C:\\Documents\\Edgar filings\\as.integer(cik[i])_8-K_year\\as.integer(cik[i])_8-K_year-month[m]-day[d].txt")  
          tdm <- TermDocumentMatrix(mydata)
          findAssocs(tdm, c("Chapter 11", "Chapter 7"), c(0.99, 0.99)) 
        }
      }
    }
  }
} 

Any help would be much appreciated. Thanks

  • 1
    Possible duplicate: https://stackoverflow.com/questions/4854130/how-to-iterate-over-file-names-in-a-r-script – MrFlick Oct 25 '17 at 19:01
  • Any help with what? – Misha Slyusarev Oct 25 '17 at 19:02
  • Also potentially helpful: https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames – MrFlick Oct 25 '17 at 19:03
  • You need to use `paste()` or `sprintf()` to create the path. Also, I'd recommend using `setwd()` to shorten the path that you're referencing in the rest of your code. – Mako212 Oct 25 '17 at 19:05
  • Possible duplicate of [How to iterate over file names in a R script?](https://stackoverflow.com/questions/4854130/how-to-iterate-over-file-names-in-a-r-script) – gaurav5430 Oct 25 '17 at 19:28

1 Answers1

1

Try this code :

temp <- list()
for(i in 1:length(list.files()))  {
        temp[[i]] <- read_file(file.path(getwd(),list.files()[i])) 
}
Paul Lemarchand
  • 1,908
  • 1
  • 12
  • 23
ist123
  • 165
  • 4