Quantcast

Creating factors

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Creating factors

Damian Betebenner-2

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


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Creating factors

Matthew Dowle
Ok I see that. Please raise a bug report for this one. Thanks.

On Sun, 2011-12-04 at 05:07 -0600, Damian Betebenner wrote:

> 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
>
>
> _______________________________________________
> datatable-help mailing list
> [hidden email]
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help


_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Loading...