not sure but try data.table::rbindlist like this (it should be faster too)
rbindlist(lapply(c("~/tmp/new.csv", "~/tmp/new2.csv"), function(x)
as.data.table(read.csv(x)))
~Malcolm
From: [email protected]
[mailto:[email protected]] On Behalf Of G See
Sent: Wednesday, November 07, 2012 3:53 PM
To: [email protected]
Subject: [datatable-help] rbinding an empty data.table and a non-empty
data.table
When I try to rbind an empty data.table to a non-empty data table, all my data
are converted to logical. Here's an example
# create a directory and put 2 csv file in it.
dir.create("~/tmp")
system("echo 'A,B' > ~/tmp/new.csv") # this csv only has headers; no data
write.csv(data.frame(A=1, B=2), row.names=FALSE, file='~/tmp/new2.csv') # this
one has header and 1 row
lapply(c("~/tmp/new.csv", "~/tmp/new2.csv"), read.csv)
#[[1]]
#[1] A B
#<0 rows> (or 0-length row.names)
#
#[[2]]
# A B
#1 1 2
# now rbind them, and we're left with the data from the non-empy csv
do.call(rbind, lapply(c("~/tmp/new.csv", "~/tmp/new2.csv"), read.csv))
# A B
#1 1 2
# Now let's try to work with data.tables instead of data.frames
lapply(c("~/tmp/new.csv", "~/tmp/new2.csv"), function(x)
as.data.table(read.csv(x)))
#[[1]]
#Empty data.table (0 rows) of 2 cols: A,B
#
#[[2]]
# A B
#1: 1 2
#Ok, but look at what happens when we rbind them
do.call(rbind, lapply(c("~/tmp/new.csv", "~/tmp/new2.csv"), function(x)
as.data.table(read.csv(x))))
A B
1: TRUE TRUE
What's going on here?
Thanks,
Garrett
_______________________________________________
datatable-help mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help