[datatable-help] Why does cbind drop key?

Matthew Dowle mdowle at mdowle.plus.com
Tue Nov 15 14:58:17 CET 2011


Just noticed you said cbind(DT,DF). That has been a saga, see FAQ 2.23,
but it's ok for now.

Ok, so we could easily change cbind.data.frame from

> cbind.data.frame
function (..., deparse.level = 1)
{
    if (inherits(..1, "data.table"))
        return(data.table(...))
    data.frame(..., check.names = FALSE)
}

to

> cbind.data.frame
function (..., deparse.level = 1)
{
    if (inherits(..1, "data.table"))
        return(data.table(...,key=key(..1)))
    data.frame(..., check.names = FALSE)
}




>
> Good point. Could you add a comment to FR#295 and perhaps embelish the
> title to include cbind please.
>
> https://r-forge.r-project.org/tracker/index.php?func=detail&aid=295&group_id=240&atid=978
>
> A reason for not setting the key attribute after cbind may have been speed
> because attr<- takes a copy of the whole object (it triggers copy-on-write
> when called within a function). At least, that's what I thought at the
> time perhaps. We now have an internal function to set attributes by
> reference, so it's pretty easy to do that now.
>
> But, := is much much better if you can.
>
>
>> When I do cbind(DT, DF), where DT has a key, the result loses the key.
>>  cbind can't change ordering though, so why does that happen?  I can
>> understand if you are trying to combine two keyed DT's that the result
>> is ambiguous, but it isn't in this case.
>> _______________________________________________
>> datatable-help mailing list
>> datatable-help at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>




More information about the datatable-help mailing list