Thank you both for response! Enh, I never think about function though. Good.
My way is using 'aggregate' and 'merge' but it is a little bit trivial. On Wed, Aug 11, 2010 at 9:13 PM, Dennis Murphy <djmu...@gmail.com> wrote: > Hi: > > Try this from package plyr: > > library(plyr) > # function to compute sum of squares > f <- function(df) with(df, sum((tapply(value, type, sum)/sum(value))^2)) > # apply it to each type of food: > ddply(data, .(food), conc = f(data)) > food type value market_con > 1 drink water 5 0.5987654 > 2 drink soda 7 0.5987654 > 3 drink soda 6 0.5987654 > 4 fruit apple 2 0.7222222 > 5 fruit apple 3 0.7222222 > 6 fruit orange 1 0.7222222 > > HTH, > Dennis > > On Wed, Aug 11, 2010 at 3:40 PM, Yi <liuyi.fe...@gmail.com> wrote: > >> Thank you for reminding me. >> >> ###### >> food=c('fruit','fruit','fruit','drink','drink','drink') >> type=c('apple','apple','orange','water','soda','soda') >> value=c(2,3,1,5,7,6) >> data=data.frame(food,type,value) >> share=c((2+3)/(2+3+1),5/6,1/6,5/(5+7+6),13/18,13/18) >> >> market_con=c(rep(0.8333333^2+0.1666667^2,3),rep(0.2777778^2+0.7222222^2,3)) >> data$market_con=market_con >> ###### >> >> Share= sum of value for specific type for some food / sum of value for all >> types for some food >> >> concentration= sum of square of share >> >> For food 1, fruit, for apple ,there are two rows, value =2 and 3; for >> oranget, value =1. >> >> share_apple=(2+3)/(2+3+1)=5/6 ## It is the same for the two rows >> share_organge=1/6 >> >> concentration_fruit= share_apple^2+sjare_orange^2=(5/6)^2+(1/6)^2 >> >> Hope this makes it easier to understand. >> >> Thanks >> >> Yi >> >> On Wed, Aug 11, 2010 at 3:33 PM, Wu Gong <w...@mtmail.mtsu.edu> wrote: >> >> > >> > Hi Yi, >> > >> > It would be helpful for others to provide a solution if you give your >> > formulas that calculating the value of share and concentration. >> > >> > ?apply will helps. >> > >> > Regards, >> > >> > Wu >> > >> > >> > >> > ----- >> > A R learner. >> > -- >> > View this message in context: >> > >> http://r.789695.n4.nabble.com/How-to-calculate-the-concentration-tp2321888p2321966.html >> > Sent from the R help mailing list archive at Nabble.com. >> > >> > ______________________________________________ >> > R-help@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<http://www.r-project.org/posting-guide.html> >> <http://www.r-project.org/posting-guide.html> >> >> > and provide commented, minimal, self-contained, reproducible code. >> > >> >> [[alternative HTML version deleted]] >> >> >> ______________________________________________ >> R-help@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<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.