首页 文章

Ifelse具有不同长度的数据帧

提问于
浏览
1

我有数据集,其中包含以下变量的面板数据:1 . 国家2.公司3.每月日期4.收入

`A <- data.frame(Country=as.factor(rep('A', 138)), 
  Company = as.factor(c(rep('AAA', 12), rep('BBB', 8), rep('CCC', 72), rep('DDD', 46))),
  Date = c(seq(as.Date('2010-01-01'), as.Date('2011-01-01'), by = 'month'), 
  seq(as.Date('2010-01-01'), as.Date('2010-07-01'), by = 'month'),
  seq(as.Date('2010-01-01'), as.Date('2015-12-01'), by = 'month'),
  seq(as.Date('2012-03-01'), as.Date('2015-12-01'), by = 'month')), 
  Revenue= sample(10000:25000, 138)
)



B<- data.frame(Country=as.factor(rep('B', 108)), 
    Company = as.factor(c(rep('EEE', 36), rep('FFF', 36), rep('GGG', 36))),
    Date = c(seq(as.Date('2013-01-01'), as.Date('2015-12-01'), by = 'month'), 
             seq(as.Date('2013-01-01'), as.Date('2015-12-01'), by = 'month'),
             seq(as.Date('2013-01-01'), as.Date('2015-12-01'), by = 'month')),
    Revenue = sample(10000:25000, 108)
)`

我想在数据集中添加其他变量 - 竞争对手的收入,即相应月份本国所有其他公司的收入总和 .

我写了以下代码:

new_B<-data.frame()
for(i in 1:nlevels(B$Company)){ 
  temp_i<-B[which(B$Company==levels(B$Company)[i]),]
  temp_j<-B[which(B$Company!=levels(B$Company)[i]),]
  agg_temp<-aggregate(temp_j$Revenue, by = list(temp_j$Date), sum)
  temp_i$competitor_value<-ifelse(agg_temp$Group.1 %in% temp_i$Date, agg_temp$x, 0)
  new_B<-rbind(new_B, temp_i)
}

我在for循环中创建了两个临时数据集,其中包含公司i和另一个 - 所有其他公司 . 我按月汇总了其他公司的所有收入 . 然后使用ifelse进行相同的日期,我将新变量添加到 temp_i . 它适用于同一时期运营的公司,但在A国,有些公司在不同时期运营,当我尝试使用我的代码时,我有错误,它们的长度不一样

new_A<-data.frame()
  for(i in 1:nlevels(A$Company)){ 
    temp_i<-A[which(A$Company==levels(A$Company)[i]),]
    temp_j<-A[which(A$Company!=levels(A$Company)[i]),]
    agg_temp<-aggregate(temp_j$Revenue, by = list(temp_j$Date), sum)
    temp_i$competitor_value<-ifelse(agg_temp$Group.1 %in% temp_i$Date, agg_temp$x, 0)
    new_A<-rbind(new_A, temp_i)
  }

我找到了类似的答案ifelse statements with dataframes of different lengths,但仍然不知道如何解决我的问题 .

我真的很感激帮助

1 回答

  • 3

    我建议使用 dplyr 包的不同方法:

    library(dplyr)
    A %>% 
      bind_rows(B) %>%
      group_by(month=format(Date, "%Y-%m")) %>% 
      mutate(revComp = sum(Revenue)) %>%
      group_by(Company, add = T) %>%
      mutate(revComp = revComp-Revenue) 
    # Source: local data frame [246 x 6]
    # Groups: month, Company [246]
    # 
    #    Country Company       Date Revenue   month revComp
    #      (chr)   (chr)     (date)   (int)   (chr)   (int)
    # 1        A     AAA 2010-01-01   10657 2010-01   30356
    # 2        A     AAA 2010-02-01   11620 2010-02   22765
    # 3        A     AAA 2010-03-01   17285 2010-03   33329
    # 4        A     AAA 2010-04-01   22886 2010-04   33469
    # 5        A     AAA 2010-05-01   20129 2010-05   39974
    # 6        A     AAA 2010-06-01   22865 2010-06   26896
    # 7        A     AAA 2010-07-01   13087 2010-07   29542
    # 8        A     AAA 2010-08-01   19451 2010-08   14842
    # 9        A     AAA 2010-09-01   12364 2010-09   15309
    # 10       A     AAA 2010-10-01   19375 2010-10   14090
    

相关问题