[datatable-help] Auto-convert characters to factors when settings keys?

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon May 24 22:32:50 CEST 2010


Hi all,

Would it make sense to autoconvert characters to factors when setting
a character column as a key?

For example, when converting a data.frame to a data.table, picking a
character column as a key throws an error:

R> df <- data.frame(a=as.character(sample(LETTERS[1:10])), b=1:10,
stringsAsFactors=F)
R> dt <- as.data.table(df)
R> key(dt) <- 'a'
Error in setkey("x", value) :
  All keyed columns must be storage mode integer

Converting dt$a to a factor before setting the key works fine, as expected:

R> dt$a <- factor(dt$a)
R> key(dt) <- 'a'

So, I'm wondering if it would make sense to auto convert character
columns to factors when calling `key` on these columns ... maybe with
a warning, perhaps ... ?

Thanks,
-steve

-- 
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