[R] summary and plot

arun smartpink111 at yahoo.com
Fri Oct 11 05:28:22 CEST 2013


Hi,
May be:
dat1<- read.table(text="city year sex  obs
1      1990  M  25
1      1990  F  32
1      1991  M  15
1      1991  F  22
2      1990  M  42
2      1990  F  36
2      1991  M  12
2      1991  F  16",sep="",header=TRUE,stringsAsFactors=FALSE)

library(plyr)
#by city
 d1 <- ddply(dat1,.(city),summarize,Tot=sum(obs))
 d2 <- ddply(dat1,.(city,sex),summarize,Tot=sum(obs))
 res <- merge(d1,d2,by="city")
 res$percent <- round((res$Tot.y/res$Tot.x)*100,1)
library(plotrix)
 plot(percent~city,data=res,type="p")
 thigmophobe.labels(res$city,res$percent,labels=res$sex)


#by year
d1 <- ddply(dat1,.(year),summarize,Tot=sum(obs))
 d2 <- ddply(dat1,.(year,sex),summarize,Tot=sum(obs))
 res2 <- merge(d1,d2,by="year")
 res2$percent <- round((res2$Tot.y/res2$Tot.x)*100,1)

plot(percent~year,data=res2,type="p",xaxt="n")
 axis(1,at=res2$year,labels=res2$year)
  thigmophobe.labels(res2$year,res2$percent,labels=res2$sex)

A.K.




On Thursday, October 10, 2013 10:37 PM, Val <valkremk at gmail.com> wrote:
Hi All,

I have a  huge data set with the following type;
  city year sex  obs
1      1990  M  25
1      1990  F   32
1      1991  M  15
1      1991  F   22
2      1990  M  42
2      1990  F  36
2      1991  M  12
2      1991  F  16

I want to calculate the percentage of M and F  by city, year and year
within city and also plot.
city 1    total Male= 40;  total female= 54;
                   %M= 40/(40+54)=42.6
                    %F= 54/(40+54)=57.4

and so on.

Can any body help me out?

Thanks in advance

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




More information about the R-help mailing list