How to write a text file through R? Creating a new file or any format say.csv , etc and writing the data generated inside that file is nothing but writing a text file in R. For instance, we create a 4*5 matrix in our example. We then pass this matrix through a dataframe. Using the write () function this matrix is written/displayed inside a new.csv file is stored inside the files folder. This recipe demonstrates an example of how to write a file.
m <- matrix(c(1:10), nrow = 4, ncol = 5, byrow = TRUE) print(m)
df <- data.frame(m)
Using the write.csv() function the matrix created gets written/displayed in the new csv file named'example file.csv'. This file can be accessed from the files folder. syntax - write.csv(data,filename) where, data - the created data filename - the name of the file in which you wish to save your data