我正在阅读csv文件并计算列并将结果附加到csv,但我无法做到 这是代码
ahg = list()
for(i in 1:3){
ahg[[i]] = " X+Y" #calculating columns
print(ahg[[i]])
write.csv(ahg[[i]],"D:/wed.csv")
}
print(ahg[[i]])
的O \ P
[1] 20
[1] 13.51
[1] 15.25
但是write.csv的O \ P,wed.csv
只给出了最后一个值,即15.25
任何暗示我做错的建议
感谢。
答案 0 :(得分:1)
您无法在文件的每一行末尾写入额外的值。这不是write.csv()
的工作方式。
你需要
append = FALSE
)。例如:
# create example CSV
data(iris)
write.csv(iris, 'test.csv', row.names=FALSE)
# ------- this is what you need to do --------
df <- read.csv('test.csv')
# create a new column
df$Petal.Area <- df$Petal.Length * df$Petal.Width
# over-write the file, but make sure you have a backup somewhere
write.csv(df, 'test.csv', row.names=FALSE)
# --- end of example -- you can delete it now ---
file.remove('test.csv')