How to append a whole dataframe to a CSV in R ? - GeeksforGeeks

30 May, 2021
A data frame in R programming language is a tabular arrangement of rows and columns arranged in the form of a table. A CSV file also contains data stored together to form rows stacked together. Content can be read from and written to the CSV file. Base R contains multiple methods to work with these files. The write.csv() method overwrites the entire contents of the file. Therefore, it results in the deletion of original CSV content. 
The modification is made to the contents of the file. If the row.names are set to TRUE, then the data may become ambiguous, since row numbers are appended to the beginning of the data and all the rows shift one position to the right side. The sep argument is necessary to discriminate between rows, otherwise incorrect results are produced. 
Syntax:
write.table(df, csv- file , append = TRUE, sep = “,”, col.names = FALSE, row.names = FALSE)
Parameters : 
df – The data frame to be appended
csv-file – The file name to append to 
append – Indicator of whether to merge to existing contents or not
col.names – Indicator of whether to append column headings to the csv.
Row numbers are appended in the beginning of the row by default, beginning from integer value of 1. 
Dataset in use:
Example:
R
# specifying the path of csv filepath <- "gfg.csv"  # read contents of filecontent1 <- read.csv(path)print ("Original content")  # displaying original contentprint (content1)  # creating data frame to appenddata_frame <- data.frame(ID = c(8:9),                         Name = c("K","L"),                         Post= c("IT","Writer"),                         Age = c(18,27))  # writing contents of the filecontent <- write.table(data_frame , path, append = T ,                        col.names = FALSE,sep = ",",                       row.names = F)  # contents of the csv filecontent2 <- read.csv(path)print ("Modified content")  # displaying modified contentprint (content2)
Output
[1] “Original content”
ID Name    Post Age
1  5    H      CA  67
2  6    K     SDE  39
3  7    Z   Admin  28 
[1] “Modified content”
ID Name    Post Age
1  5    H      CA  67
2  6    K     SDE  39
3  7    Z   Admin  28
4  8    K      IT  18
5  9    L  Writer  27
In case, the col.names argument is set to TRUE, the column headings are appended as a row before the data. This leads to the display of column headings twice, and one extra row is returned in the result. The column names of the data frame may or may not be same as row headers of CSV file. 
Example:
R
path <- "gfg.csv"  content1 <- read.csv(path)print ("Original content")print (content1)  # creating data frame to appenddata_frame <- data.frame(ID = c(8:9),Name = c("K","L"),                         Post= c("IT","Writer"),Age = c(18,27))  # writing contents of the filecontent <- write.table(data_frame , path, append = T ,                        col.names = TRUE,sep = ",", row.names = F)  # contents of the csv filecontent2 <- read.csv(path)print ("Modified content")print (content2)
Output
[1] “Original content”
ID Name    Post Age
1  5    H      CA  67
2  6    K     SDE  39
3  7    Z   Admin  28
[1] “Modified content”
  ID Name    Post  Age 
1  5    H      CA   67 
2  6    K     SDE   39 
3  7    Z   Admin   28 
4 ID Name    Post  Age 
5  8    K      IT   18 
6  9    L  Writer   27
Picked
R-CSV
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
R - if statement
Time Series Analysis in R
How to filter R dataframe by multiple conditions?