append to a csv file in r

To append data to a CSV file in R, you can use the write.table() function with the append = TRUE argument. The write.table() function writes a data frame to a file in tabular format. Here's an example:

main.r
# sample data frame
df <- data.frame(name = c("Alice", "Bob", "Charlie"),
                 age = c(25, 30, 35))

# write the data frame to a CSV file
write.table(df, file = "data.csv", sep = ",", row.names = FALSE)

# append new data to the CSV file
new_data <- data.frame(name = "Dave", age = 40)
write.table(new_data, file = "data.csv", sep = ",", row.names = FALSE, append = TRUE)
384 chars
11 lines

This code will create a CSV file called "data.csv" in the working directory (if it doesn't already exist), write the df data frame to it, and then append the new_data data frame to it. The final contents of "data.csv" will be:

main.r
name,age
Alice,25
Bob,30
Charlie,35
Dave,40
44 chars
6 lines

gistlibby LogSnag