How to write a csv through R?

This recipe helps you write a csv through R

Recipe Objective

CSV stands for Comma Separated Value. .csv is a text file which is used to store tabular data where usually text is separated by comma. It is quite widely used as a data source where each line is a data record.

R gives you the platform to read, write and edit data that are stored in any external environment.

In this recipe we will discuss how to write a CSV file in R.

Step 1: Creating a Dataframe

We use data.frame() function to create a dataframe using vectors.

data = data.frame(Name = c("Nikhil", "Fredo", "Sam"), AGE = c("15", "34", "45"), Hobbies = c("Football", "Reading", "Swimming")) ​
data
Nikhil	15	Football
Fredo	34	Reading
Sam	45	Swimming

Step 2: Writing a CSV file

We use write.csv() function to write csv file.

Syntax: write.csv(x, file, row.names, quote)

where:

  1. x = dataframe that you want to write;
  2. file = name along with the extension .csv;
  3. row.names = (Optional) Input FALSE if you don't want row labels in your CSV file. By default, it's TRUE
  4. quote = (Optional) Input FALSE if you don't want double quotes in text content. By default, it's TRUE
write.csv(data, "dftocsv.csv", quote = FALSE)

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.