Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

How to append a whole dataframe to a CSV in R

I can already append a row to a CSV using cat which makes that very easy:

  cat("my row, 1, 2, 3, 4", "mydf.csv",sep="
", append=TRUE)

However as far as I know you cannot use cat with a whole dataframe (multiple columns and rows).

I am doing this because I am writing many DFs to a CSV and I want to append the multiple CSVs using write.table. The dataframes all have the same number of columns.

I thought about doing a loop over rows to write with cat but that doesn't sound like the best way - any one have good way of doing this in R?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok so I realised that append=T does work with write.table - but write.table needs the sep switch to be used so this works:

write.table(myDF, "myDF.csv", sep = ",", col.names = !file.exists("myDF.csv"), append = T)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...