write.csv在将数据帧写入磁盘时会弄乱标题名称

时间:2017-02-15 12:06:06

标签: r csv export-to-csv

我正在使用write.csv写一堆数据帧:

data_frames <- Filter(function(x) is(x, "data.frame"), mget(ls()))

for (i in 1:length(data_frames)) {

 file_name<- paste0("data/to_save/",names(data_frames[i]),".csv")

 write.csv(data_frames[i], file = file_name, row.names = FALSE, col.names = TRUE)

} 

问题是标题是乱码添加文件名到这样的每个colname(用记事本打开文件):

"April2016_file.customer_id","April2016_file.customerName","April2016_file.customerSurname".......

使用write.table并使用来自包export的{​​{1}}脚本的相同行为

必填结果:

rio

1 个答案:

答案 0 :(得分:1)

我们需要使用list提取[[元素,因为[仍为listdata.frame

for (i in seq_along(data_frames)) {
  file_name<- paste0("data/to_save/",names(data_frames[i]),".csv")
   write.csv(data_frames[[i]], file = file_name, row.names = FALSE, col.names = TRUE)
}