0

I got a list of files containing the same xml structure and I have a function, parsing this structure into a dataframe. In pseudocode, what I want to do is the following:

listOfFiles listFiles(diretoryPath)
foreach file in listOfFiles
   parse file to dataFrame 
   and add dataFrame to listOfDataFrames
combine listOfDataFrames to one dataframe

My first approach looked like this:

files<-list.files(path,full.names = TRUE)
dataFrames<-sapply(files,function(file){[...]return dataframe})
finalDataFrame<- rbind(dataFrames)

Which didn't work, since sapply returns a large matrix and not a list of dataframes.

Any good ideas how to do this elegantly in R?

Oblomov
  • 8,953
  • 22
  • 60
  • 106
  • Use lapply instead of sapply. Adding the parameter simplify=FALSE in the call to sapply might work too but using lapply would just be the better route. – Dason Feb 03 '15 at 12:54
  • This doesn't seem to have anything to do with elegant ways to parse XML. We could use a bit more info about the problem – Rich Scriven Feb 03 '15 at 13:06
  • using lapply doesn't help: I still get a large matrix instead of a list of dataframes. – Oblomov Feb 03 '15 at 13:10
  • Where's your code? There are specific functions in the XML package to handle these situations. One cannot simply parse xml with `sapply()` – Rich Scriven Feb 03 '15 at 13:13
  • I have replaced the actual parsing code by the placeholder [...] in my example since the parsing function works and I don't think its relevant for this particular problem. The function could be any function returning a dataframe with the same column number and column names. – Oblomov Feb 03 '15 at 13:37
  • Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Otherwise your problem is impossible to diagnose. – shadow Feb 03 '15 at 14:24

0 Answers0