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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.