如何在R中的向量中获取全局环境变量?

时间:2017-10-20 17:09:03

标签: r dataframe vector

我有一个csv数据文件,其中50000条记录存储在数据帧“数据”中。我正在根据2个因素创建数据子集Segment&市场价值低于:

customer_segments <- c('Consumer','Corporate','Home Office')
markets <- c('Africa','APAC','Canada','EMEA','EU','LATAM','US')

为Market&amp; amp;获得21种组合的所有子集Segement,我在嵌套for循环下使用assign&amp;粘贴功能:

for(i in 1:length(markets)){
  for(j in 1:length(customer_segments)){
assign(paste(markets[i],customer_segments[j],sep='_'),data[(data$Market == markets[i]) & (data$Segment == customer_segments[j]), ])
  }
}

这创建了21个数据帧&amp;相应地为他们指定一个名称Canada_Home Office等。 问题是我想迭代所有这21个数据帧来聚合3个属性:Sales,Quantity&amp;获利但不确定如何在循环中处理这些数据帧?也许如果我在一个向量中得到所有21个数据帧,我可以迭代,但不确定这是否是最好的选择。

1 个答案:

答案 0 :(得分:0)

使用markets创建customer_segmentsexpand.grid()的组合。

  df <- expand.grid(markets, customer_segments)
  head(df)
  #      Var1        Var2
  # 1  Africa    Consumer
  # 2    APAC    Consumer
  # 3  Canada    Consumer
  # 4    EMEA    Consumer
  # 5      EU    Consumer
  # 6   LATAM    Consumer

marketscustomer_segments

组合的向量
  df1 <- as.vector(paste(df$Var1,df$Var2, sep = " "))
  df1
  # [1] "Africa Consumer"    "APAC Consumer"      "Canada Consumer"   
  # [4] "EMEA Consumer"      "EU Consumer"        "LATAM Consumer"    
  # [7] "US Consumer"        "Africa Corporate"   "APAC Corporate"    
  # [10] "Canada Corporate"   "EMEA Corporate"     "EU Corporate"      
  # [13] "LATAM Corporate"    "US Corporate"       "Africa Home Office"
  # [16] "APAC Home Office"   "Canada Home Office" "EMEA Home Office"  
  # [19] "EU Home Office"     "LATAM Home Office"  "US Home Office"