【问题标题】:All possible subsets of set集合的所有可能子集
【发布时间】:2014-10-31 14:30:44
【问题描述】:

我的环境:目前是 Win7 和 R 3.1.1。

我有某个公司的品牌和相应市场份额的数据集: (Apple, 0.50), (Nokia, 0.24), (HTC,0.12), (RIM, 0.07), (Palm, 0.03) 等等......

所以我买了一套,比如(Apple、Nokia、HTC、RIM,...)。

我想要所有可能的集合子集,其中包含其组件的市场份额总和。

例如,具有值 (0.50+0.12) 的子集 (Apple,HTC)。

我怎样才能实现这个想法?

任何帮助将不胜感激。

【问题讨论】:

  • 您需要在此处提供更多信息 - 您的输入 data.frame 是什么样的?输出呢?你试过什么?
  • 两个您可能会觉得有用的函数:?expand.grid?combn - 但正如 John 所说,需要更多信息来帮助您

标签: r subset


【解决方案1】:

感谢 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)

【讨论】:

    【解决方案2】:

    试试这个:

    # sample data
    df <- data.frame(company = c('Apple','Nokia','HTC','RIM','Palm'),
                    share = c(.5,.24,.12,.07,.03))
    
    # 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 with correct classes
    data<-as.data.frame(t(result),stringsAsFactors = F)
    data$V2 <- as.numeric(data$V2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多