[datatable-help] rbinding an empty data.table and a non-empty data.table

G See gsee000 at gmail.com
Wed Nov 7 22:53:28 CET 2012


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20121107/e8966d99/attachment.html>


More information about the datatable-help mailing list