[R] by group

Val valkremk at gmail.com
Wed Oct 21 00:49:24 CEST 2015


Hi All,

Thank you very  much for your suggestion to use ddply function for my data
set. I used the function like the following

mydata <- read.table(header=TRUE, text='
                             cntry  state city gender
                               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
                            ')

 cndata <- ddply(mydata, c("cntry","gender"), summarise,
                N    = length(gender)
  )
 cndata

cntry gender N
   1      1  3
   1      2  4

My question now is,  i want mdata to  be as follows
cntry  sex1(F) sex2 (M)
 1          3         4

Then I want And I want merge cndata with  the original data,  mydata. How
do I do that?

Thank you oin advance.






On Mon, Oct 12, 2015 at 5:27 PM, MCGUIRE, Rhydwyn <
rmcgu at doh.health.nsw.gov.au> wrote:

> 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.
>
> _______________________________________________________________________________________________________
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list