2

我有一个数据集,其中包含对许多网站上进行的客户调查的多个标准的反馈评论,其中每一行代表一个响应。

为简单起见,我简化了原始数据集并生成了一个可重现的数据框,其中仅包含三个站点的评论。

标准从第 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)基于此提取,对于每个 unique SITE,我想创建一个包含三张工作表的 Excel 文件,每张工作表以该给定站点的底部 3 个标准命名。

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

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

地区_站点_Comments2017.xlsx

所需的最终输出:

3 个 Excel 文件(或与唯一站点一样多的文件),每个 Excel 文件具有三个以其底部 3 个标准命名的选项卡,每个工作表都有一个与该站点的给定标准相对应的评论列表。

例如,生成的三个文件之一如下所示:

  • 文件名是 ASIA_TokyoCenter_Comments2017.xlsx
  • 该文件将包含 3 张纸,“照明”、“卫生”和“动机”(基于本网站的三个底部标准)
  • 这些工作表中的每一个都将包含它们各自的站点级注释。

我的方法论:

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

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

但是,我似乎无法获得预期的结果。我在 Stackoverflow 上搜索过类似的解决方案,但还没有找到任何东西。

对此的任何帮助将不胜感激!

4

1 回答 1

1

这可能可以更好地格式化......但是对于区域和站点中的每个级别,对于每个“底部”,我们提取每个独立的组合并写入文件。

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)
       }
     }
   }
 }

这是你要找的吗?

于 2017-11-29T10:07:50.490 回答