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

G See gsee000 at gmail.com
Wed Nov 7 23:08:51 CET 2012


Thanks. I actually discovered it using rbindlist; it suffers the same
problem.

Garrett


On Wed, Nov 7, 2012 at 4:07 PM, Cook, Malcolm <MEC at stowers.org> wrote:

> 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:* datatable-help-bounces at lists.r-forge.r-project.org [mailto:
> datatable-help-bounces at lists.r-forge.r-project.org] *On Behalf Of *G See
> *Sent:* Wednesday, November 07, 2012 3:53 PM
> *To:* datatable-help at lists.r-forge.r-project.org
> *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****
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20121107/b2d6fe67/attachment.html>


More information about the datatable-help mailing list