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 [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
