[R] by group

MCGUIRE, Rhydwyn rmcgu at doh.health.nsw.gov.au
Tue Oct 13 00:27:20 CEST 2015


ddply() is what you are looking for. Here is an example http://www.inside-r.org/packages/cran/plyr/docs/ddply

Cheers,
Rhydwyn 


Rhydwyn McGuire
Senior Biostatistician | Health Statistics NSW
Level 7, 73 Miller St, North Sydney 2060
Tel 02 9391 9781 | rmcgu at doh.health.nsw.gov.au
www.health.nsw.gov.au



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Val
Sent: Tuesday, 13 October 2015 4:06 AM
To: r-help at r-project.org
Subject: [R] by group

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]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
__________________________________________________________________________________________________________
This email has been scanned for the NSW Ministry of Health by the Websense Hosted Email Security System.
Emails and attachments are monitored to ensure compliance with the NSW Ministry of health's Electronic Messaging Policy.
__________________________________________________________________________________________________________
_______________________________________________________________________________________________________
Disclaimer: This message is intended for the addressee named and may contain confidential information.
If you are not the intended recipient, please delete it and notify the sender.
Views expressed in this message are those of the individual sender, and are not necessarily the views of the NSW Ministry of Health.
_______________________________________________________________________________________________________
This email has been scanned for the NSW Ministry of Health by the Websense Hosted Email Security System.
Emails and attachments are monitored to ensure compliance with the NSW Ministry of Health's Electronic Messaging Policy.



More information about the R-help mailing list