Open In App

How to Export DataFrame to CSV in R ?

Improve
Improve
Like Article
Like
Save
Share
Report

R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language.

Approach: 

  • Write Data in column wise format
  • Create DataFrame for these data
  • Write Data to the CSV file
  • Print the success message

Function used:

  • dataframe() is used to create data frame

Syntax:

data.frame(column1, column2, column3)

  • write.csv() function is used create csv file from the data set.

Syntax:

 write.csv(dataframe, path) 

Example:

R




Rollno <- c("5", "6", "7")
Name <- c("John Doe","Jane Doe", "Bill Gates")
Marks <- c("80", "75", "95")
Age <- c("13", "13", "14")
  
  
df <- data.frame(Rollno,Name,Marks,Age)
  
write.csv(df,"C:\\Users\\...YOUR PATH...\\agedata.csv", row.names = FALSE)
  
print ('CSV created Successfully :)')


Output: 

Example:

R




Country <- c("China", "India", "United States", "Indonesia", "Pakistan")
  
Population <- c("1,407,068,600","1,374,506,881", "331,329,046"
                "270,203,917", "225,200,000")
  
Date <- c("15 Mar 2021", "15 Mar 2021", "15 Mar 2021"
          "30 Sep 2020", "1 Jul 2021")
  
df <- data.frame(Country, Population, Date)
  
write.csv(df,"C:\\Users\\...YOUR PATH...\\population.csv", row.names = FALSE)
  
print ('CSV created Successfully :)')


Output: 



Last Updated : 26 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads