How to append a whole dataframe to a CSV in R ?
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 file path <- "gfg.csv" # read contents of file content1 <- read.csv (path) print ( "Original content" ) # displaying original content print (content1) # creating data frame to append data_frame <- data.frame (ID = c (8:9), Name = c ( "K" , "L" ), Post= c ( "IT" , "Writer" ), Age = c (18,27)) # writing contents of the file content <- write.table (data_frame , path, append = T , col.names = FALSE ,sep = "," , row.names = F) # contents of the csv file content2 <- read.csv (path) print ( "Modified content" ) # displaying 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 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 append data_frame <- data.frame (ID = c (8:9),Name = c ( "K" , "L" ), Post= c ( "IT" , "Writer" ),Age = c (18,27)) # writing contents of the file content <- write.table (data_frame , path, append = T , col.names = TRUE ,sep = "," , row.names = F) # contents of the csv file content2 <- 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