[datatable-help] Creating factors

Damian Betebenner dbetebenner at nciea.org
Sun Dec 4 12:07:34 CET 2011


All,

Finding some inconsistent behaviors in how data.table deals with variable assignment depending upon whether a $ or [[ ]] is used.
I'm using data.table 1.7.4



require(data.table)

### Using $

tmp.dt <- data.table(X=1:5)
tmp.dt$Y <- factor(1, levels=1:2, labels=c("VALID_CASE", "INVALID_CASE"))

> tmp.dt
     X Y
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 1
[5,] 5 1

> sapply(tmp.dt, class)
        X         Y
"integer" "integer"

### Using [[

tmp.dt <- data.table(X=1:5)
tmp.dt[["Y"]] <- factor(1, levels=1:2, labels=c("VALID_CASE", "INVALID_CASE"))

> tmp.dt
     X          Y
[1,] 1 VALID_CASE
[2,] 2 VALID_CASE
[3,] 3 VALID_CASE
[4,] 4 VALID_CASE
[5,] 5 VALID_CASE
> sapply(tmp.dt, class)
        X         Y
"integer"  "factor"


Thoughts,

Damian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20111204/110a3b0a/attachment.htm>


More information about the datatable-help mailing list