【发布时间】:2021-11-03 19:04:22
【问题描述】:
我需要使用 cat 函数向 CSV 添加新行。请大家帮帮我好吗?我对 R 的了解有限。
这是文件“name1.csv”,我被要求将我的姓名和学生 ID 添加到前几行。
homework1 <- data.frame (homework1,Total)
homework1 <- data.frame (homework1, Commission)
# Create output file name using name1. All your output will go to this file.
sink("name1.csv")
# send the output to the csv file you just created
write.csv(homework1, file = "name1.csv")
# delete first column of the csv file
# Use cat function to write your name (First Last). This will be Row 1 of csv file.
cat(file = "name1.csv", "Ibra", "\n", sep = ",", append=TRUE, row.names(F))
【问题讨论】:
-
cat(file='name1.csv',"Name\nID") -
@Waldi 这不会覆盖整个文件吗?
-
@MartinGal,你是对的,这取决于如何理解
add;-)。cat(file='name1.csv',"Name\nID",append=T)应该修复它 -
谢谢你们!它终于可以工作了...知道如何将名称和 ID 添加到 csv 文件的顶部而不是底部吗?
-
请提供足够的代码,以便其他人更好地理解或重现问题。