[R] Weighting column entries in a data frame

camillus citizencamillus at gmail.com
Tue Jun 23 21:11:31 CEST 2009


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.




More information about the R-help mailing list