[R] How to calculate the concentration

Wu Gong wg2f at mtmail.mtsu.edu
Thu Aug 12 01:38:32 CEST 2010


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(rep(0.8333333^2+0.1666667^2,3),rep(0.2777778^2+0.7222222^2,3))
data$market_con=market_con
###### 

# ?rowsum help you understand the function


data <- data[order(data$food,data$type),] # Sort data by food and type
data$typesum <- rep(rowsum(data$value,data$type,reorder =
F),rle(as.character(data$type))$lengths)
data$foodsum <- rep(rowsum(data$value,data$food,reorder =
F),rle(as.character(data$food))$lengths)
data$share <- data$typesum/data$foodsum
data$sharesquare <- data$share^2

new.data <- unique(data[,-(3:6)])
concentration <- rowsum(new.data$sharesquare,new.data$food)

-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-concentration-tp2321888p2322007.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list