[datatable-help] calculating proportions on 192 column data table grouped by one column

François Morneau francois.morneau at ign.fr
Wed Jul 27 11:22:09 CEST 2016


Hello Carl,

Will dt1[, lapply(.SD, function(x) x / sum(x))] return what you want ?

Regards,

François

Le 18/07/2016 à 05:43, carlsutton a écrit :
> I can do the calculation on a couple columns, but how do I automate this for
> 192 column data table?
>
> #  calculating proportion for column variables
> library(data.table)
> a <- rep(1:5, 2)
> b <- sample(20:50, size = 10, replace = TRUE)
> c <- sample(80:130, size = 10, replace = TRUE)
> dt1 <- data.table(a,b,c)
> dt1
> d <- sum(dt1$b)
> e <- sum(dt1$c)
> proportion_b <- dt1$b/d
> proportion_c <- dt1$c/e
> proportion_b
> proportion_c
> dt_manipulated <- data.table(dt1,proportion_b, proportion_c)
> dt_manipulated
> #  now an attempt to add column proportions to the data table
> dt2 <- dt1[, `:=` (proportion = b/sum(b), proportion = c/sum(c))]
> identical(dt_manipulated,dt2)
> #  Nice that this works, but I sure do NOT want to do this for 191 columns
> of data!
> #  My instinct is to use lapply, but how do I define the column to divide by
> column sum???
>
>
>
>
>
> -----
> Carl Sutton
> --
> View this message in context: http://r.789695.n4.nabble.com/calculating-proportions-on-192-column-data-table-grouped-by-one-column-tp4722828.html
> Sent from the datatable-help mailing list archive at Nabble.com.
> _______________________________________________
> 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