Re: [R] How to calculate the concentration

2010-08-12 Thread Yi
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 wrote: > Hi: > > Try this from package plyr: > > library(plyr) > # function to compute sum of sq

Re: [R] How to calculate the concentration

2010-08-11 Thread Dennis Murphy
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 drin

Re: [R] How to calculate the concentration

2010-08-11 Thread Wu Gong
Hi, The code seems complicate, but it's understandable. ## 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(re

Re: [R] How to calculate the concentration

2010-08-11 Thread Yi
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.833^2+0.167^2,3),r

Re: [R] How to calculate the concentration

2010-08-11 Thread Wu Gong
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-

[R] How to calculate the concentration

2010-08-11 Thread Yi
Hi, folks, ## 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.833^2+0.167^2,3),rep(0.278^2+0