感谢 ARobertson,我快到了,这就是我现在得到的。
df <- data.frame(company = c('B','m1','m2','m3','m4','m5','m6','m7'),
share = c(0, 0.235, 0.252, 0.063, 0.073, 0.069, 0.022, 0.286))
# create index of all combinations
allcombsindex <- lapply(1:nrow(df),function(x){
combn(1:length(df$company),x,simplify = F)
})
# get rid of extra level
allcombsindex <- do.call('c',allcombsindex)
# paste together company names and sum the shares
result <- sapply(allcombsindex,function(x,y = df){
c(paste(y$company[x],collapse = ","),
sum(y$share[x]))
})
# transpose upright
data<-as.data.frame(t(result))
# from Factor into Numeric, see the class(data$V2)
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
# Define market share summation
m<-as.numeric.factor(data$V2)
# And now good looking data frame
data_working<-data.frame(data$V1,m)
# the limit repertory
# r=0.5, s=0.75
m0<-0.5*(1-0.75)/(1-0.5*0.75)
data_working2<-data.frame(data_working, m0)
但我想进步,因为这是我目标的一半。
首先,我需要比较data_working2的一行中的m和m0。
其次,根据m和m0的比较,我想返回m或者0。
我已经试过了:
compare<-function(m,m0){if (m > m0) return(m) else return (0)}
data2<-apply(data1, 1, compare)
它失败了。但是这个成功了!
# compare the limit repertoty with existing repertory
compare<-ifelse(data_working2[,2]>data_working2[,3],data_working2[,2],0)