- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save a csv and read using fread in R?
To save a csv file, we can use write.csv function in R and if we want to read it using fread then fread function will be used with the name of the csv file. The benefit of reading the csv file with fread function is that there will be a variable added to the original csv file which contains the id as integers starting from 1 to the length of column values.
Consider the below data frame −
Example
x<-rpois(20,5) y<-rpois(20,5) z<-rpois(20,5) df<-data.frame(x,y,z) df
Output
x y z 1 8 2 4 2 2 5 4 3 5 5 3 4 7 3 9 5 3 3 7 6 1 10 6 7 5 5 3 8 4 5 3 9 5 5 2 10 8 9 7 11 6 5 6 12 8 4 3 13 6 5 10 14 4 5 3 15 4 5 7 16 6 5 6 17 6 4 6 18 2 8 7 19 6 4 3 20 8 5 6
Saving the above data frame in csv file −
write.csv(df,"FreadReadingEx.csv")
Reading the csv file using fread function −
Example
fread("FreadReadingEx.csv")
Output
V1 x y z 1: 1 8 2 4 2: 2 2 5 4 3: 3 5 5 3 4: 4 7 3 9 5: 5 3 3 7 6: 6 1 10 6 7: 7 5 5 3 8: 8 4 5 3 9: 9 5 5 2 10: 10 8 9 7 11: 11 6 5 6 12: 12 8 4 3 13: 13 6 5 10 14: 14 4 5 3 15: 15 4 5 7 16: 16 6 5 6 17: 17 6 4 6 18: 18 2 8 7 19: 19 6 4 3 20: 20 8 5 6
- Related Articles
- How to save a matrix as CSV file using R?
- How to save a vector in R as CSV file?
- How to read data from *.CSV file using JavaScript?
- How to save a Python Dictionary to CSV file?
- How to read and parse CSV files in C++?
- How to read CSV file in Python?
- How to read CSV files in Golang?
- How to save HTML Tables data to CSV in Python
- How to read data from .csv file in Java?
- How to read a Pandas CSV file with no header?
- How to save an xtable file locally using R?
- How to create a blank csv file in R?
- How to read the data from a CSV file in Java?\n
- How to read a CSV file and store the values into an array in C#?
- How to save a plot in pdf in R?

Advertisements