R根据另一个数据集的条件从数据集生成多个Excel文件

时间:2017-11-29 09:31:57

标签: r r-xlsx

我在许多网站上进行的客户调查中得到了一个包含多个条件反馈意见的数据集,其中每行代表一个响应。

为了简单起见,我简化了原始数据集,并生成了一个可重现的数据框,只有三个网站的评论。

标准列于第4-10列。

comments = data.frame(RESPONDENT_ID=c(1,2,3,4,5,6,7,8),
             REGION=c("ASIA","ASIA","ASIA","ASIA","ASIA","EUROPE","EUROPE","EUROPE"),
             SITE=c("Tokyo Center","Tokyo Center","Tokyo Center","PB Tower","PB Tower","Rome Heights","Rome Heights","Rome Heights"),
             Lighting=c("Dim needs to be better","","Good","I don't like it","Could be better","","",""),
             Cleanliness=c("","very clean I'm happy","great work","","disappointed","I like the work","","nice"),
             Hygiene=c("","happy","needs improvement","great","poor not happy","nice!!","clean as usual i'm never disappointed",""),
             Service=c("great service","impressed","could do better","","","need to see more","cant say","meh"),
             Punctuality=c("always on time","","loving it","proper and respectful","","","punctual as always","delays all the time!"),
             Efficiency=c("generally efficient","never","cannot comment","","","","","happy with this"),
             Motivation=c("always very motivated","driven","exceeds expectations","","poor service","ok can do better","hmm","motivated"))

我有第二个数据集,其中包含三个网站中每个网站的最低3个评分标准。

bottom = data.frame(REGION=c("ASIA","ASIA","EUROPE"),
             SITE=c("Tokyo Center","PB Tower","Rome Heights"),
             BOTTOM_1=c("Lighting","Cleanliness","Motivation"),
             BOTTOM_2=c("Hygiene","Service","Lighting"),
             BOTTOM_3=c("Motivation","Punctuality","Cleanliness"))                 

我的目标:

1)在comments数据框中,对于每个SITE,我想过滤bottom数据框,并仅为每个网站提取最低3个条件的评论

2)基于此提取,对于每个唯一SITE,我想创建一个包含三张纸的Excel文件,每张纸以该给定网站的底部3条件命名。

3)每张工作表都包含为该特定网站提取的评论列表。

4)我希望以格式保存所有Excel文件:

  

REGION _ SITE _Comments2017.xlsx

期望的最终结果:

3个Excel文件(或与唯一站点一样多的文件),每个Excel文件有三个以最低3个标准命名的选项卡,每个工作表都有一个与该站点的给定标准对应的注释列表。

举个例子,生成的三个文件中的一个看起来像这样:

  • 文件名为ASIA_TokyoCenter_Comments2017.xlsx
  • 该文件将包含3张纸,"照明","卫生" &安培; "动机" (基于本网站的三个最低标准)
  • 这些工作表中的每一个都包含各自的网站级注释。

我的方法论

我尝试在for数据框上使用comments循环,并为列出的每个网站过滤bottom数据框。

然后使用write.xlsx包中的xlsx函数生成Excel文件,并为每个站点的每个底部三个citeria设置sheetName参数。

然而,我似乎无法获得理想的结果。我在Stackoverflow上搜索了类似的解决方案,但还没找到任何东西。

对此有任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:1)

这可能格式化得更好...... 但是对于Region和Site中的每个级别,对于每个' bottom',我们提取每个独立的组合并写入文件。

bottom <- sapply(bottom, as.character) # Get out of factors.
sp <- split(comments, comments$REGION) # Split data into a list format for ease.
for(i in unique(bottom[,1])){
   for(j in unique(bottom[,2])){
       x <- sp[[1]][sp[[i]][,3]==j,]
       y <-  x[,colnames(x)%in%bottom[bottom[,1]==i& bottom[,2]==j,3:5]]
       for(q in colnames(y)){
       if(nrow(x) > 0) {
         write.xlsx(x=y[,q],
                    file=paste(i,j, 'Comments2017.xlsx', sep='_'),
                    sheetName=q, append=T)
       }
     }
   }
 }

这是你在找什么?