[datatable-help] Wonder whether there is an easier way to change part of data.table values

Branson Owen branson.owen at gmail.com
Thu Jul 29 20:38:52 CEST 2010


I thought I have no more question, but ... Please take your time to
respond, I don't want to overwhelm your time.

** I want to only change values for certain rows of selected columns. **

In data.frame, I can do something like:

> DF[row index, "column"] = new value.

In data.table, this has been disabled even using "with = FALSE"

>DT[3,"Z", with = FALSE]

       Z
[1,] 20

> DT[3,"Z", with = FALSE] <- 1
Error in `[<-.data.table`(`*tmp*`, 3, "Z", with = FALSE, value = 1) :
  unused argument(s) (with = FALSE)


Actually, I found that there is no way I can edit value using [,] in
DT. The only way I found to change value is using DT$column[index] =
new value

This would make the following task difficult:

# DOES NOT WORK #
> DT[join/select, {
columnA <- calculation based on columnB, C, D, ...
}]
# DOES NOT WORK #

It didn't complain, but it doesn't change value at all. I guess this
is due to the syntax of with in data.frame because it doesn't work
there, either.

At this moment, my solution is:
> DT[join/select, {
DT$columnA[index] <<- calculation based on columnB, C, D, ...
}]

with the help of DT$columnA[index] and super assign <<-. We also need
to either get index by ourselves like DT[select/join, which = T] or
store it first. Not sure whether this is the best solution.

In DF, it would be

> index = using scan
> DF[index, columnA] = with(DF, calculation based on columnB, C, D, ...)

Note that this doesn't work for DT. At this moment, the only way I
found to edit DT is
> DT$column[index] = new value

I don't think my example is uncommon, but I can't find common solution
using data.table. Maybe, I missed something.

Any comments will be highly appreciated. Thank you very much again for
your help.

Best regards,


More information about the datatable-help mailing list