[datatable-help] setkey removes attribute settings

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Jun 9 04:34:12 CEST 2011


Hi,

On Wed, Jun 8, 2011 at 7:01 PM, Thell Fowler <tbfowler4 at gmail.com> wrote:
>
> It looks like custom attributes are lost by a DF to DT creation, and when the key info for a DT is invalidated, altered, etc...  I ran into this when trying to attach some attribute info to a table that identifies column groups so that a new global variable or passing another param to functions wouldn't be needed.  This worked nicely, until I had need to either modify some data in the table, which invalidates the key...
>
> Was this a design decision?  If so, why?

Try `as.data.table` for a more "delicate" way to convert a data.frame
to a data.table. Your attributes will come through to the other side.

R> df <- data.frame( id=c(1:5), name= letters[1:5] )
R> attr(df, 'testing') <- 'keep me'
R> dt <- as.data.table(df)
R> attr(dt, 'testing')
[1] "keep me"

-steve

>
> Example::
>
> > df <- data.frame( id=c(1:5), name= letters[1:5] )
>
> > attr(df, 'testing') <- 'keep me'
>
> > attr(df, 'testing')
> [1] "keep me"
>
> > dt <- data.table( df )
> > attr(dt, 'testing')
>
> NULL
> > attr(dt, 'testing') <- 'keep me'
>
> > attr(dt, 'testing')
> [1] "keep me"
>
> > df[ 5 , 'id' ] <- 53
> > dt[ 5 , 'id' ] <- 53
>
> > attr(df, 'testing')
> [1] "keep me"
>
> > attr(dt, 'testing')
> [1] "keep me"
>
> > setkey(dt)
> > attr(dt, 'testing')
>
> NULL
> > attr(dt, 'testing') <- 'keep me'
>
> > attr(dt, 'testing')
> [1] "keep me"
>
> > dt[ 5 , 'id' ] <- 3
> > attr(dt, 'testing')
>
> [1] "keep me"
> > key(dt)
> NULL
>
> >
> --
> Sincerely,
> Thell
>
> _______________________________________________
> 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
>



--
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the datatable-help mailing list