📜  如何在 R 中将 DataFrame 导出为 CSV?

📅  最后修改于: 2022-05-13 01:55:21.633000             🧑  作者: Mango

如何在 R 中将 DataFrame 导出为 CSV?

R 编程语言允许我们将数据读取和写入各种文件,如 CSV、Excel、XML 等。在本文中,我们将讨论如何使用 R 编程语言将 DataFrame 导出为 CSV 文件。

方法:

  • 以列格式写入数据
  • 为这些数据创建 DataFrame
  • 将数据写入 CSV 文件
  • 打印成功信息

使用的函数:

  • dataframe() 用于创建数据框

句法:

  • write.csv()函数用于从数据集创建 csv 文件。

句法:

例子:

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 :)')


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 :)')


输出:

例子:

电阻

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 :)')

输出: