英文:
How to append new rows into existing csv files where columns are in different order
问题
I have 4个CSV文件,每周都会获得新的原始数据。我想将新数据追加到我存储在仓库中的现有4个CSV文件报告中。问题在于,当我使用write.table或write.csv并使用参数"append = TRUE"时,列不匹配,因此我的行被错位。
for (report in names(clean_reports)) {
data <- clean_reports[[report]]
write.table(data,
file = paste(report,".csv",sep = ""),
append = TRUE,
col.names = FALSE,
row.names = FALSE,
sep = ",")
}
我原本希望根据列名将列追加到正确的位置。显然,write.table不会读取现有的列。不确定是否有其他解决方法。
英文:
I have 4 csv files and every week I get new raw data. I want to append the new data into the existing 4 csv file reports I have in a repository. the problem is that when I use either write.table or write.csv with the parameter "append = TRUE" the columns aren't matched therefore my rows are displaced.
for (report in names(clean_reports)) {
data <- clean_reports[[report]]
write.table(data,
file = paste(report,".csv",sep = ""),
append = TRUE,
col.names = FALSE,
row.names = FALSE,
sep = ",")
}
I was expecting the columns appended into the correct position based on column name. Apparently write.table doesn't read the existing columns. Not sure what other workaround there is
答案1
得分: 1
当使用 write.table 或 write.csv 函数时,若参数设置为 append = TRUE,列不会自动根据列名进行匹配。相反,新数据将仅附加到现有文件中,而不考虑列结构。
要实现附加新数据并确保列匹配的期望结果,可以按照以下步骤操作:
将现有的CSV文件读取到单独的数据框中。
将新数据附加到相应的数据框中。
将更新后的数据框写回CSV文件。
# 文件名列表
file_names <- c("report1.csv", "report2.csv", "report3.csv", "report4.csv")
# 将现有CSV文件读入数据框中
existing_data <- lapply(file_names, read.csv)
# 循环遍历报告并附加新数据
for (i in seq_along(file_names)) {
report <- file_names[i]
data <- clean_reports[[report]] # 假设 clean_reports 是包含新数据的列表
# 将新数据附加到现有数据框中
if (exists(report, envir = .GlobalEnv)) {
existing_data[[i]] <- rbind(existing_data[[i]], data)
} else {
existing_data[[i]] <- data
}
# 将更新后的数据框写回CSV文件
write.csv(existing_data[[i]], file = report, row.names = FALSE)
}
在这段代码中,使用 read.csv 将现有的CSV文件读取到单独的数据框中。然后,对于每个报告,使用 rbind 将新数据附加到相应的现有数据框中。如果报告数据框尚不存在,则会使用新数据创建它。最后,使用 write.csv 将更新后的数据框写回CSV文件,覆盖现有文件。如需更多帮助,请随时提问。
英文:
When using write.table or write.csv with the append = TRUE parameter, the columns are not automatically matched based on column names. Instead, the new data is simply appended to the existing file without considering the column structure.
To achieve the desired result of appending new data while ensuring the columns match, you can follow these steps:
Read the existing CSV files into separate data frames.
Append the new data to the corresponding data frames.
Write the updated data frames back to the CSV files.
# List of file names
file_names <- c("report1.csv", "report2.csv", "report3.csv", "report4.csv")
# Read existing CSV files into data frames
existing_data <- lapply(file_names, read.csv)
# Loop through the reports and append new data
for (i in seq_along(file_names)) {
report <- file_names[i]
data <- clean_reports[[report]] # Assuming clean_reports is a list with new data
# Append new data to existing data frame
if (exists(report, envir = .GlobalEnv)) {
existing_data[[i]] <- rbind(existing_data[[i]], data)
} else {
existing_data[[i]] <- data
}
# Write the updated data frame back to the CSV file
write.csv(existing_data[[i]], file = report, row.names = FALSE)
}
In this code, the existing CSV files are read into separate data frames using read.csv. Then, for each report, the new data is appended to the corresponding existing data frame using rbind. If the report data frame doesn't exist yet, it is created with the new data. Finally, the updated data frame is written back to the CSV file using write.csv, overwriting the existing file. Feel free to ask for clarification if you need more assistance Gerardo! Feliz día! No olvides votar esta respuesta si asi lo deseas
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论