[R] by group

Val valkremk at gmail.com
Mon Oct 12 19:06:08 CEST 2015


Hi all,


Assume that I have the following data set :

 cntry  state city Gender (1=F and 2=M)

 1 1 1   1
 1 1 1   2
 1 1 1   1
 1 1 2   2
 1 2 2   2
 1 2 3   2
 1 2 3   1

I want to calculate the number of Females and Males,
total (F+M) and  percentage (F/M) by country, state and city.

Here is the  sample of the output file  that I would like to have in a file.

cntry state City Gender
Cntry_F Cntry_M    Cntry_total 100 (Cntry_F/Cntry_M)
St_F    st_M    St_total  100*(   St_F/   St_M)
City_F     City_M     City_total  100*( City_F/ City_M)

 1  1  1  1   3  4  7  75    2  2  4  50   1 1 3  33
 1  1  1  2   3  4  7  75    2  2  4  50   1 1 3  33
 1  1  1  1   3  4  7  75    2  2  4  50   1 1 3  33
 1  1  2  2   3  4  7  75    2  2  4  50   2 0 2 100
 1  2  2  2   3  4  7  75    2  1  3  67   2 0 2 100
 1  2  3  2   3  4  7  75    2  1  3  67   1 1 2  50
 1  2  3  1   3  4  7  75    2  1  3  67   1 1 2  50

Your help is highly appreciated in advance

	[[alternative HTML version deleted]]



More information about the R-help mailing list