[datatable-help] aggregate data.table by group by reference
Gerhard Nachtmann
kpm.nachtmann at gmail.com
Wed Mar 7 16:38:20 CET 2018
Hi!
You didn't provide an reproducable example (repex), so I created one
for you including the solution.
Consider you want to add the median values for each column of the iris
dataset by species.
iris <- as.data.table(iris)
iris[iris[, lapply(.SD, median), keyby = "Species"],
paste0(names(iris), ".med") := get(paste0("i.", names(iris))), on =
"Species"]
Cheers,
Gerhard
2018-03-07 11:04 GMT+01:00 Matthew Forrest <matthew.forrest at senckenberg.de>:
> Hi all,
>
> A real data.table question here ;-)
>
> I am currently doing this:
>
> output.dt <- input.dt[,lapply(.SD, method.function), by=by.dims]
>
> where method.function is an arbitrary function and by.dims is a list of
> columns (although perhaps I could remove this is the data.table was keyed by
> those columns?)
>
> That doesn't look very data.table-y to me, potentially it would be great to
> do it with := in the proper data.table way. Is that possible? I have tried
> a couple of things but haven't managed. The problem is that I need to be
> flexible to deal with arbitrary columns in my data.table.
>
> Any suggestions?
>
> Many thanks,
>
> Matt
>
>
> --
> Dr Matthew Forrest
> Senckenberg Biodiversity and Climate Research Centre (BiK-F)
> Visiting address: Georg-Voigt-Straße 14-16, room 3.04, Frankfurt am Main
> Postal address: Senckenberganlage 25
> D-60325 Frankfurt am Main
> Tel.: +49-69-7542-1867
> Fax: +49-69-7542-7904
> E-mail: matthew.forrest at senckenberg.de
> Homepage: www.bik-f.de
>
> Senckenberg Gesellschaft für Naturforschung
> Rechtsfähiger Verein gemäß § 22 BGB
> Senckenberganlage 25
> 60325 Frankfurt
>
> Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Andreas Mulch,
> Stephanie Schwedhelm, Prof. Dr. Katrin Böhning-Gaese, Prof. Dr. Uwe Fritz,
> PD Dr. Ingrid Kröncke
>
> Präsidentin: Dr. h.c. Beate Heraeus
>
> Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)
>
> _______________________________________________
> 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