-1

I have a bunch of .csv files located in the path path which I import into a list of dataframes named datalist using the following code:

require(gsubfn) ## to use strapply

datalist <- list()
files <- list.files(path)

for(file in files) {
    stem <- gsub("\\.csv$","",file)
    datalist[[stem]] <- data.frame(read.delim(file, sep = ";", header=TRUE))
    month <- strapply(stem,"^([^_]*).*$")
    year <- strapply(stem,"^[^_]*_([^_]*)_.*$")
    datalist[[stem]]$Month <- month
    datalist[[stem]]$Year <- year
}

(As you may have noticed, I also grab the month and year the data refer to using two regular expressions, but this shouldn't cause the problem I encounter later.)

Then I row-bind all dataframes into a single dataframe named merged using the rbind function from the plyr package:

require(plyr) ## to use rbind

merged <- rbind.fill(datalist)

My problem is that the dataframe I end up with appears to be a strange dataframe of lists: indeed, if I try to export it into a .csv format, R throws me the following error:

write.csv(merged,'merged.csv')

Error in write.table(x, file, nrow(x), p, rnames, sep, eol, na, dec, as.integer(quote),: 
unimplemented type 'list' in 'EncodeElement'

And another similar error is thrown at me if I try to reshape the dataframe using the melt and cast functions from the reshape package:

require(reshape) ## to use melt and cast
molten <- melt(merged)

cast <- cast(molten, ...formula...)

Error in order(var1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, : 
unimplemented type 'list' in 'listgreater'

So, how can I obtain a normal dataframe from a list of dataframe? I think the problem is in the "importing" process, as the same error unimplemented type 'list' in 'EncodeElement' is thrown at me even if I try to export to .csv just one single dataframe in the datalist list.

Thanks a lot.

Wilco
  • 429
  • 2
  • 5
  • 13
  • 3
    I see that the output of `strapply` is a list. I think you can resolve the issue with `as.character(strapply(...))` – bdemarest Dec 02 '12 at 20:25
  • Yes, this was it! I just noticed that the problem was in those two variables I created with `strapply`. If you write this as an answer it'll be the accepted one. Thanks. – Wilco Dec 02 '12 at 21:33

2 Answers2

1

I just ran across a new-to-me solution to this problem at http://www.r-bloggers.com/concatenating-a-list-of-data-frames/ . The data.table package contains a function called rbindlist() that will (quoting from the docs) take "a list containing data.table, data.frame or list objects" and bind them together into a single data.frame, matching columns either by position or by name. It looks like you could get what you're aiming for with

install.packages("data.table")
library(data.table)
merged<-rbindlist(datalist,use.names=TRUE)

rbindlist() was apparently written in C rather than R, and it's got a noticeable speed advantage over the plyr library according to the r-bloggers.com article linked above.

dspitzle
  • 740
  • 2
  • 9
  • 26
  • 1
    Are you sure this solves the issue experienced by OP? – Heroka Dec 01 '15 at 16:33
  • Without access to datalist I can't guarantee there won't be a problem, but I've just used it for the same sort of consolidation of files into a data.frame. I suspect the strapply() problem noted above would cause problems with this, though. – dspitzle Dec 01 '15 at 19:19
0

So long as all the data.frames in the list have the same headings, you can use plyr.

library(plyr) 
dat <- ldply(your_list)

For example:

dat <- data.frame(A=1:10,B=rnorm(10))
alistofdataframes <- list()
alistofdataframes$group1 <- dat
alistofdataframes$group2 <- dat
ldply(alistofdataframes)
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • `rbind.fill` is a faster way of doing what `ldply` does, if I am not mistaken: look [here](http://stackoverflow.com/a/2851434/1081468). Using `ldply` is slower and gives me exactly the same error. – Wilco Dec 02 '12 at 19:16