Skip to content
Related Articles

Related Articles

Improve Article

How to Export DataFrame to CSV in R ?

  • Last Updated : 26 Mar, 2021

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: 




My Personal Notes arrow_drop_up
Recommended Articles
Page :