How to write a text file through R?

This recipe helps you write a text file through R

Recipe Objective

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.

Step 1 -Creating a 4*5 matrix in a vector m

m <- matrix(c(1:10), nrow = 4, ncol = 5, byrow = TRUE) print(m)

Step 2 -Create a dataframe for the matrix

df <- data.frame(m)

Step 3 -Write a text file

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

write.csv(df,"example file.csv")

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

MLOps using Azure Devops to Deploy a Classification Model
In this MLOps Azure project, you will learn how to deploy a classification machine learning model to predict the customer's license status on Azure through scalable CI/CD ML pipelines.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

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.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.