R 根据另一个数据集的条件从数据集生成多个 Excel 文件
R generate multiple Excel files from a dataset, based on conditions from another
我有一个数据集,其中包含来自在许多网站上进行的客户调查的多个标准的反馈评论,其中每一行代表一个响应。
为简单起见,我简化了原始数据集并生成了一个可重现的数据框,其中仅包含三个站点的评论。
标准列在第 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"))
我的Objective:
1) 从 comments
数据框,对于每个 SITE
,我想过滤 bottom
数据框,并仅提取每个站点后 3 个标准的评论.
2) 基于这个提取,对于每个唯一的 SITE
,我想创建一个包含三个 sheet 的 Excel 文件,每个 sheet 命名为在该给定网站的最后 3 个条件之后。
3) 每个 Sheet 将包含为该特定站点提取的评论列表。
4) 我希望所有 Excel 文件都以以下格式保存:
REGION_SITE_Comments2017.xlsx
期望的最终输出:
3 Excel 个文件(或与唯一站点一样多的文件),每个 Excel 文件都有三个选项卡,这些选项卡以其底部的 3 个标准命名,每个 sheet 都有一个与该站点的给定标准相对应的评论列表。
例如,生成的三个文件之一如下所示:
- 文件名将是 ASIA_TokyoCenter_Comments2017.xlsx
- 该文件将包含 3 sheets,"Lighting","Hygiene" & "Motivation"(基于本网站的三个底部标准)
- 这些 sheet 中的每一个都将包含它们各自的站点级评论。
我的方法论:
我尝试在 comments
数据帧上使用 for
循环,并为列出的每个站点过滤 bottom
数据帧。
然后使用 xlsx
包中的 write.xlsx
函数生成 Excel 文件,并将 sheetName
参数设置为每个站点底部的三个标准.
但是我似乎得不到想要的结果。我在 Whosebug 上搜索了类似的解决方案,但还没有找到任何东西。
如有任何帮助,我们将不胜感激!
这可能可以格式化得更好...
但是对于 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)
}
}
}
}
这是您要找的吗?
我有一个数据集,其中包含来自在许多网站上进行的客户调查的多个标准的反馈评论,其中每一行代表一个响应。
为简单起见,我简化了原始数据集并生成了一个可重现的数据框,其中仅包含三个站点的评论。
标准列在第 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"))
我的Objective:
1) 从 comments
数据框,对于每个 SITE
,我想过滤 bottom
数据框,并仅提取每个站点后 3 个标准的评论.
2) 基于这个提取,对于每个唯一的 SITE
,我想创建一个包含三个 sheet 的 Excel 文件,每个 sheet 命名为在该给定网站的最后 3 个条件之后。
3) 每个 Sheet 将包含为该特定站点提取的评论列表。
4) 我希望所有 Excel 文件都以以下格式保存:
REGION_SITE_Comments2017.xlsx
期望的最终输出:
3 Excel 个文件(或与唯一站点一样多的文件),每个 Excel 文件都有三个选项卡,这些选项卡以其底部的 3 个标准命名,每个 sheet 都有一个与该站点的给定标准相对应的评论列表。
例如,生成的三个文件之一如下所示:
- 文件名将是 ASIA_TokyoCenter_Comments2017.xlsx
- 该文件将包含 3 sheets,"Lighting","Hygiene" & "Motivation"(基于本网站的三个底部标准)
- 这些 sheet 中的每一个都将包含它们各自的站点级评论。
我的方法论:
我尝试在 comments
数据帧上使用 for
循环,并为列出的每个站点过滤 bottom
数据帧。
然后使用 xlsx
包中的 write.xlsx
函数生成 Excel 文件,并将 sheetName
参数设置为每个站点底部的三个标准.
但是我似乎得不到想要的结果。我在 Whosebug 上搜索了类似的解决方案,但还没有找到任何东西。
如有任何帮助,我们将不胜感激!
这可能可以格式化得更好... 但是对于 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)
}
}
}
}
这是您要找的吗?