[datatable-help] No error on invalid assignment

Matthew Dowle mdowle at mdowle.plus.com
Sat Oct 29 02:53:14 CEST 2011


On Fri, 2011-10-28 at 18:46 -0400, Stavros Macrakis wrote:
> > t1 <- data.table(a=1:3,b=1/(1:3))
> > t1[1,2]<- 99
> 
> 
> Shouldn't this give an error?  
Why? What else could it mean other than what it does :
> t1
     a         b
[1,] 1 1.0000000
[2,] 2 0.5000000
[3,] 3 0.3333333
> t1[1,2]<- 99
> t1
     a          b
[1,] 1 99.0000000
[2,] 2  0.5000000
[3,] 3  0.3333333


> With with=T (the default), this doesn't make sense, and the resulting
> DT is not valid:

The [<- method is different to the [ method of data.table.

> 
> 
> > t1
> Error in data.table(lapply(x, function(x) { : 
>   every input must have at least one value, unless all columns are
> empty

I can't reproduce this. I got a different (correct) error :
> t1[1,2,with=FALSE]<- 99
Error in `[<-.data.table`(`*tmp*`, 1, 2, with = FALSE, value = 99) : 
  unused argument(s) (with = FALSE)


> 
> 
> In fact, I'm not sure how to modify an individual cell in a DT:

It already did, above.  But, := is faster and shorter than <-.

> 
> 
> > t1 <- data.table(a=1:3,b=1/(1:3))
> > t1[1,2,with=F]<- 99
> Error in `[<-.data.table`(`*tmp*`, 1, 2, with = F, value = 99) : 
>   unused argument(s) (with = F)
> 
> 
> On the other hand, you can apparently modify an entire column:
> 
> 
> t1 <- data.table(a=1:3,b=1/(1:3))
> > t1$b <- 11:13
> > t1
>      a  b
> [1,] 1 11
> [2,] 2 12
> [3,] 3 13
> 
> 
> And modifying a key column very sensibly makes it no longer a key:
> 
> 
> > t2 <- data.table(a=1:3,b=1/(1:3),key="a")
> > key(t2)
> [1] "a"
> > t2$a <- 11:13
> > key(t2)
> NULL
> 

Yes, all intended and correct, iiuc.

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