[datatable-help] := unclarity and possible bug?
Chris Neff
caneff at gmail.com
Thu Aug 4 14:45:20 CEST 2011
Yes I am using the latest version with type="source" now (just forgot
to update today before sending off the message) and that makes the
second question about multiple := fixed, but the first one is still
relevant.
On 4 August 2011 08:38, Matthew Dowle <mdowle at mdowle.plus.com> wrote:
> I suspect you don't have the latest version of 1.6.3, see the commit log.
>
> Perhaps subscribe to datatable-commits, so you know when to re-install from
> R-Forge using type="source", and try again. I won't suggest waiting for
> CRAN as you're already ok using latest from R-Forge (which is now 1.6.4).
>
> Matthew
>
> "Chris Neff" <caneff at gmail.com> wrote in message
> news:CAAuY0RVA4yLjb9xuOJNdq5ca=EGRiLvsve-FNsj2CjNL2nLoyA at mail.gmail.com...
> A second question while I'm playing with it. It seems from the FRs
> that it doesn't support multiple := in one select, but:
>
> DT <- data.table(x=1:10, y=rep(1:2,10))
> DT$a = 0
> DT$z = 0
>
> DT[, list(a := y/sum(y), z := 5)]
>
> works just fine for me. An error gets thrown but afterwards the
> columns are modified as intended. Why the error?
>
>> DT[,list(z:=5,a:=y/sum(y))]
> z
> [1] 5
> [1] TRUE
> a
> y/sum(y)
> [1] TRUE
> Error in data.table(`:=`(z, 5), `:=`(a, y/sum(y))) :
> column or argument 1 is NULL
>> DT
> x y z a
> [1,] 1 1 5 0.06666667
> [2,] 2 2 5 0.13333333
> [3,] 3 1 5 0.06666667
> [4,] 4 2 5 0.13333333
> [5,] 5 1 5 0.06666667
> [6,] 6 2 5 0.13333333
> [7,] 7 1 5 0.06666667
> [8,] 8 2 5 0.13333333
> [9,] 9 1 5 0.06666667
> [10,] 10 2 5 0.13333333
>
> -Chris
>
> On 4 August 2011 08:12, Chris Neff <caneff at gmail.com> wrote:
>> Hi all,
>>
>> If I do:
>>
>> DT <- data.table(x=1:10, y=rep(1:2,5))
>>
>> Then try the following
>>
>> DT[, z:=5]
>>
>> I get:
>>
>>> DT[, z:=5]
>> z
>> [1] 5
>> [1] TRUE
>> NULL
>>
>> and if I were to do DT <- DT[, z:=5], then DT gets set to NULL.
>> Alternatively if I do
>>
>> DT[1:10, z:=5]
>>
>> I get
>>
>>> DT=DT[1:nrow(DT),z:=5]
>> z
>> [1] 5
>> [1] 1 2 3 4 5 6 7 8 9 10
>> Error in `:=`(z, 5) :
>> Attempt to add new column(s) and set subset of rows at the same
>> time. Create the new column(s) first, and then you'll be able to
>> assign to a subset. If i is set to 1:nrow(x) then please remove that
>> (no need, it's faster without).
>>
>>
>> Which is more informative. So I do as it instructs:
>>
>> DT$z <- NA
>>
>> DT[, z:=5]
>>
>> And as output I get:
>>
>>> DT
>> x y z
>> [1,] 1 1 TRUE
>> [2,] 2 2 TRUE
>> [3,] 3 1 TRUE
>> [4,] 4 2 TRUE
>> [5,] 5 1 TRUE
>> [6,] 6 2 TRUE
>> [7,] 7 1 TRUE
>> [8,] 8 2 TRUE
>> [9,] 9 1 TRUE
>> [10,] 10 2 TRUE
>>
>>
>> Why isn't z 5 like assigned? I think it is because I assigned it as
>> NA, and data table didn't know to change it to integer (although why
>> it changed it to logical is another puzzle). If I instead do
>>
>> DT$z <- 0
>>
>> DT[, z:=5]
>>
>> It works fine.
>>
>> So my two points are:
>>
>> A) Doing DT[,z:=5] should be as informative as doing DT[1:nrow(DT),
>> z:=5] with the error message.
>>
>> B) What went wrong with the NA assignment I did?
>>
>> Thanks!
>> Chris
>>
>
>
>
> _______________________________________________
> 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