【发布时间】:2016-06-18 07:08:50
【问题描述】:
我有四个不同格式和变量的 csv 文件,使用下面的代码将这 4 个 CSV 文件组合成一个 excel 文件
library(rJava)
library(xlsx)
rm(list = ls())
# getting the path of all reports (they are in csv format)
files <- list.files(pattern = "\\.csv$")
# creating work book
wb <- createWorkbook()
# going through each csv file
for (item in files)
{
# create a sheet in the workbook
sheet <- createSheet(wb, sheetName=strsplit(item,"[.]")[[1]][1])
# add the data to the new sheet
addDataFrame(read.csv(item), sheet,row.names=FALSE)
}
# saving the workbook
saveWorkbook(wb, "crosstabs of data.xlsx")
在 csv 文件一张表中,变量名称是 source / Medium 但它在输出 excel 文件中显示为 Source...Medium, % New Sessions 变量显示为 X..New.Sessions 和所有变量分隔的空间占用。在输出excel文件中 如何克服这个我需要 CSV 文件中的变量名称与输出 Excel 文件中的变量名称相同
【问题讨论】:
-
问题来自
read.csv,它将在文件头内部使用make.names,以在R中为变量提供适合的名称。您可以尝试在read.csv的调用中指定参数check.names=FALSE