[datatable-help] colwise division

Christoph Jäckel christoph.jaeckel at wi.tum.de
Mon Jan 16 17:18:21 CET 2012


This here is a simple implementation of what you are asking for. Note that
I renamed your data.table from "a" to "DT" and that I changed the first
value of b to 0.5, so that one can easily check if the index generation is
working:

DT <- data.table(year = 2001:2010,
                 b = c(0.5, 2:10),
                 c = 11:20,
                 d = 21:30)

DT[, db := d/b]
DT[, cb := c/b]
DT[, index_b := b/b[1]]
DT[, index_c := c/b[1]]

So basically, I just use the := operator all the time, which is a handy
feature. You can check out the help page by typing ?':=' in the R console.

Does this would you want to do? Or were you looking for a more automatic
way of doing this? If so, it would help if you would be more specific about
how your real use case looks like.

 Christoph

On Mon, Jan 16, 2012 at 3:38 PM, Serdar Akin <
serdar.akin at pensionsmyndigheten.se> wrote:

>  Hi,
>
> I have a problem that I want to divide column “b” with all the other
> columns, in this case “c” and “d” , is there a nice way to do this?
>
> Further I would like to divide columns “b,c,d” with row 1 form column “b”
> to create a index?
>
>
> a <- data.table(year = 2001:2010, b = 1:10, c = 11:20, d = 21:30)
>
>
> Regards Serdar
>
>
>
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20120116/5c25c208/attachment.htm>


More information about the datatable-help mailing list