Hi Guys, I would like to weight column entries in a data frame by the population of each state. For example, here is some data: state statenum year income popul ALABAMA 1 1 9.703193 3973.00 ALABAMA 1 2 9.745950 3992.00 ALABAMA 1 3 9.762092 4015.00 ALASKA 2 1 10.221640 532.00 ALASKA 2 2 10.169600 544.00 ALASKA 2 3 10.101300 539.00 I would like to adjust "income" by population ("popul") for each state. I can use: statepop<-tapply(fdata$popul,fdata$state,sum), which gives: ALABAMA ALASKA 53590.130 7435.263 I can find the weights: popweight <- statepops/sum(statepops) ALABAMA ALASKA 0.016327564 0.002265338 Is there an efficient way to multiply each entry of "income" by "popweght" conditional on the state? That is, ALABAMA income for year 1 is 9.703193*0.016327564, etc. Thanks, William -- View this message in context: http://www.nabble.com/Weighting-column-entries-in-a-data-frame-tp24171435p24171435.html Sent from the R help mailing list archive at Nabble.com.
Try this:> fdatastate statenum year income popul 1 ALABAMA 1 1 9.703193 3973 2 ALABAMA 1 2 9.745950 3992 3 ALABAMA 1 3 9.762092 4015 4 ALASKA 2 1 10.221640 532 5 ALASKA 2 2 10.169600 544 6 ALASKA 2 3 10.101300 539> statepops<-tapply(fdata$popul,fdata$state,sum) > popweight <- statepops/sum(statepops) > fdata$adj <- fdata$income * popweight[fdata$state] > fdatastate statenum year income popul adj 1 ALABAMA 1 1 9.703193 3973 8.550515 2 ALABAMA 1 2 9.745950 3992 8.588193 3 ALABAMA 1 3 9.762092 4015 8.602417 4 ALASKA 2 1 10.221640 532 1.214266 5 ALASKA 2 2 10.169600 544 1.208084 6 ALASKA 2 3 10.101300 539 1.199971>On Tue, Jun 23, 2009 at 3:11 PM, camillus <citizencamillus@gmail.com> wrote:> > Hi Guys, > > I would like to weight column entries in a data frame by the population of > each state. For example, here is some data: > > state statenum year income popul > ALABAMA 1 1 9.703193 3973.00 > ALABAMA 1 2 9.745950 3992.00 > ALABAMA 1 3 9.762092 4015.00 > ALASKA 2 1 10.221640 532.00 > ALASKA 2 2 10.169600 544.00 > ALASKA 2 3 10.101300 539.00 > > > I would like to adjust "income" by population ("popul") for each state. I > can use: > > statepop<-tapply(fdata$popul,fdata$state,sum), > > which gives: > > ALABAMA ALASKA > 53590.130 7435.263 > > I can find the weights: > > popweight <- statepops/sum(statepops) > > ALABAMA ALASKA > 0.016327564 0.002265338 > > Is there an efficient way to multiply each entry of "income" by "popweght" > conditional on the state? That is, ALABAMA income for year 1 is > 9.703193*0.016327564, etc. > > Thanks, > > William > -- > View this message in context: > http://www.nabble.com/Weighting-column-entries-in-a-data-frame-tp24171435p24171435.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Thanks! jholtman wrote:> > Try this: > >> fdata > state statenum year income popul > 1 ALABAMA 1 1 9.703193 3973 > 2 ALABAMA 1 2 9.745950 3992 > 3 ALABAMA 1 3 9.762092 4015 > 4 ALASKA 2 1 10.221640 532 > 5 ALASKA 2 2 10.169600 544 > 6 ALASKA 2 3 10.101300 539 >> statepops<-tapply(fdata$popul,fdata$state,sum) >> popweight <- statepops/sum(statepops) >> fdata$adj <- fdata$income * popweight[fdata$state] >> fdata > state statenum year income popul adj > 1 ALABAMA 1 1 9.703193 3973 8.550515 > 2 ALABAMA 1 2 9.745950 3992 8.588193 > 3 ALABAMA 1 3 9.762092 4015 8.602417 > 4 ALASKA 2 1 10.221640 532 1.214266 > 5 ALASKA 2 2 10.169600 544 1.208084 > 6 ALASKA 2 3 10.101300 539 1.199971 >> > > > On Tue, Jun 23, 2009 at 3:11 PM, camillus <citizencamillus at gmail.com> > wrote: > >> >> Hi Guys, >> >> I would like to weight column entries in a data frame by the population >> of >> each state. For example, here is some data: >> >> state statenum year income popul >> ALABAMA 1 1 9.703193 3973.00 >> ALABAMA 1 2 9.745950 3992.00 >> ALABAMA 1 3 9.762092 4015.00 >> ALASKA 2 1 10.221640 532.00 >> ALASKA 2 2 10.169600 544.00 >> ALASKA 2 3 10.101300 539.00 >> >> >> I would like to adjust "income" by population ("popul") for each state. >> I >> can use: >> >> statepop<-tapply(fdata$popul,fdata$state,sum), >> >> which gives: >> >> ALABAMA ALASKA >> 53590.130 7435.263 >> >> I can find the weights: >> >> popweight <- statepops/sum(statepops) >> >> ALABAMA ALASKA >> 0.016327564 0.002265338 >> >> Is there an efficient way to multiply each entry of "income" by >> "popweght" >> conditional on the state? That is, ALABAMA income for year 1 is >> 9.703193*0.016327564, etc. >> >> Thanks, >> >> William >> -- >> View this message in context: >> http://www.nabble.com/Weighting-column-entries-in-a-data-frame-tp24171435p24171435.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Weighting-column-entries-in-a-data-frame-tp24171435p24190249.html Sent from the R help mailing list archive at Nabble.com.
Apparently Analagous Threads
- [LLVMdev] [llvm-commits] [PATCH] Refactoring the DFA generator
- [LLVMdev] [llvm-commits] [PATCH] Refactoring the DFA generator
- [LLVMdev] [llvm-commits] [PATCH] Refactoring the DFA generator
- [LLVMdev] [llvm-commits] [PATCH] Refactoring the DFA generator
- [LLVMdev] [llvm-commits] [PATCH] Refactoring the DFA generator