R language can read and write data into various file formats like csv, excel xml etc. In this R programming tutorial, I’ll show you how to write csv in r. Here I’ll also include simple example to demonstrate this concept clearly.
In R programming language, we can create csv file from existing data frame. To create the csv file, we need to use write.csv() in r.
# Create a data frame. data <- read.csv("input.csv") retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01")) # Write filtered data into a new file. write.csv(retval,"output.csv") newdata <- read.csv("output.csv") print(newdata)
After executing the above code, our output looks like:
X id name salary start_date dept 1 3 3 Michelle 611.00 2014-11-15 IT 2 4 4 Ryan 729.00 2014-05-11 HR 3 5 5 Gary 843.25 2015-03-27 Finance 4 8 8 Guru 722.50 2014-06-17 Finance
Above X column comes from the data set newper. While writing the file, we can dropped it using additional parameter row.names = FALSE
# Create a data frame. data <- read.csv("input.csv") retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01")) # Write filtered data into a new file. write.csv(retval,"output.csv", row.names = FALSE) newdata <- read.csv("output.csv") print(newdata)
After executing the above code, our output looks like:
id name salary start_date dept 1 3 Michelle 611.00 2014-11-15 IT 2 4 Ryan 729.00 2014-05-11 HR 3 5 Gary 843.25 2015-03-27 Finance 4 8 Guru 722.50 2014-06-17 Finance
Related Post:
How to Read CSV in R?
How to Read text file in R?
Today we have learned about how to write csv in R programming language. I hope you guys understood everything clearly. So, guys, that’s all about for today. Later we will discuss another topic of r programming language. Till then, take care. Happy Coding