<div dir="ltr">Hello All,<div><br></div><div>I am sure there is a much more efficient way to do this. Please advise any suggestions.</div><div>For now, I have boot fixed this the crude way :-(</div><div><br></div><div><div>age_brackets <- c("pop_0:pop_3","pop_4:pop_6","pop_7:pop_9")</div><div><br></div><div>for (i in age_brackets) {</div><div><span class="" style="white-space:pre"> </span>cmdText <- paste('dt[, paste("",i,sep=""):= rowSums(.SD, na.rm=TRUE), by=list(origin, race, sex,year, total_pop), .SDcols=',i,']', sep="")</div><div><span class="" style="white-space:pre"> </span>print(cmdText)</div><div><span class="" style="white-space:pre"> </span>eval(parse(text=cmdText))</div><div>}</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 8, 2015 at 11:13 PM, Santosh Srinivas <span dir="ltr"><<a href="mailto:santosh.srinivas@gmail.com" target="_blank">santosh.srinivas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello All,<div><br></div><div>I have a dataset as below with a reproducible example after that. My actual data has about 100 columns.</div><div><br></div><div>I want columns that represent the rowSums for sets .. eg. pop_0_3, pop_4_6, pop_7_9 .. this is sum of population in age group of 0-3 for example.</div><div><br></div><div>How can I do that using indexes of the columns?</div><div><br></div><div>---------------------------------------------------------------------------------------------------------------------------------------------------------<br></div><div><br></div><div><div> origin race sex year total_pop pop_0 pop_1 pop_2 pop_3 pop_4 pop_5 pop_6 pop_7 pop_8 pop_9</div><div> 1: 0 0 0 2014 318748017 3971847 3957864 3972081 4003272 4001929 4002977 4132455 4152653 4118628 4105776</div><div> 2: 0 0 0 2015 321368864 4000831 3988161 3974109 3986357 4015656 4013264 4013790 4142998 4163270 4129322</div><div> 3: 0 0 0 2016 323995528 4029356 4017346 4004585 3988434 3998839 4026967 4024121 4024481 4153686 4174008</div><div> 4: 0 0 0 2017 326625791 4057231 4046063 4033932 4019069 4000955 4010232 4037777 4034839 4035311 4164487</div><div> 5: 0 0 0 2018 329256465 4083375 4074132 4062816 4048550 4031712 4012371 4021117 4048454 4045696 4046249</div><div> 6: 0 0 0 2019 331883986 4107606 4100469 4091055 4077589 4061316 4043229 4023269 4031853 4059256 4056646</div><div> 7: 0 0 0 2020 334503458 4128810 4124893 4117546 4105953 4090466 4072931 4054223 4034013 4042721 4070166</div><div> 8: 0 0 0 2021 337108968 4145903 4146269 4142090 4132527 4118898 4102128 4083950 4065004 4044832 4053623</div><div> 9: 0 0 0 2022 339698079 4159190 4163587 4163657 4157230 4145600 4130675 4113256 4094835 4075940 4055771</div><div>10: 0 0 0 2023 342267302 4169856 4177093 4181156 4178958 4170441 4157505 4141921 4124243 4105873 4086972</div></div><div><br></div><div><br></div><div>---------------------------------------------------------------------------------------------------------------------------------------------------------<br></div><div><br></div><div><br></div><div><div># <a href="https://www.census.gov/population/projections/files/downloadables/NP2014_D1.csv" target="_blank">https://www.census.gov/population/projections/files/downloadables/NP2014_D1.csv</a></div><div><br></div><div>require("data.table")</div><div><br></div><div>dt <- structure(list(origin = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, </div><div>0L), race = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), sex = c(0L, </div><div>0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), year = 2014:2023, total_pop = c(318748017L, </div><div>321368864L, 323995528L, 326625791L, 329256465L, 331883986L, 334503458L, </div><div>337108968L, 339698079L, 342267302L), pop_0 = c(3971847L, 4000831L, </div><div>4029356L, 4057231L, 4083375L, 4107606L, 4128810L, 4145903L, 4159190L, </div><div>4169856L), pop_1 = c(3957864L, 3988161L, 4017346L, 4046063L, </div><div>4074132L, 4100469L, 4124893L, 4146269L, 4163587L, 4177093L), </div><div> pop_2 = c(3972081L, 3974109L, 4004585L, 4033932L, 4062816L, </div><div> 4091055L, 4117546L, 4142090L, 4163657L, 4181156L), pop_3 = c(4003272L, </div><div> 3986357L, 3988434L, 4019069L, 4048550L, 4077589L, 4105953L, </div><div> 4132527L, 4157230L, 4178958L), pop_4 = c(4001929L, 4015656L, </div><div> 3998839L, 4000955L, 4031712L, 4061316L, 4090466L, 4118898L, </div><div> 4145600L, 4170441L), pop_5 = c(4002977L, 4013264L, 4026967L, </div><div> 4010232L, 4012371L, 4043229L, 4072931L, 4102128L, 4130675L, </div><div> 4157505L), pop_6 = c(4132455L, 4013790L, 4024121L, 4037777L, </div><div> 4021117L, 4023269L, 4054223L, 4083950L, 4113256L, 4141921L</div><div> ), pop_7 = c(4152653L, 4142998L, 4024481L, 4034839L, 4048454L, </div><div> 4031853L, 4034013L, 4065004L, 4094835L, 4124243L), pop_8 = c(4118628L, </div><div> 4163270L, 4153686L, 4035311L, 4045696L, 4059256L, 4042721L, </div><div> 4044832L, 4075940L, 4105873L), pop_9 = c(4105776L, 4129322L, </div><div> 4174008L, 4164487L, 4046249L, 4056646L, 4070166L, 4053623L, </div><div> 4055771L, 4086972L)), .Names = c("origin", "race", "sex", </div><div>"year", "total_pop", "pop_0", "pop_1", "pop_2", "pop_3", "pop_4", </div><div>"pop_5", "pop_6", "pop_7", "pop_8", "pop_9"), class = c("data.table", </div><div>"data.frame"), row.names = c(NA, -10L))</div></div><div><br></div><div><br></div><div>---------------------------------------------------------------------------------------------------------------------------------------------------------<br></div><div><br></div><div>Thank you.</div><span class="HOEnZb"><font color="#888888"><div>Santosh</div></font></span></div>
</blockquote></div><br></div>