从 R 编程中的脚本导出数据
到目前为止,使用 R 程序的操作是在没有存储在任何地方的提示/终端上完成的。但是在软件行业,大多数程序都是为了存储从程序中获取的信息而编写的。一种这样的方法是将获取的信息存储在文件中。因此,可以对文件执行的两个最常见的操作是:
- 将数据导入 R 脚本
- 从 R 脚本导出数据
从 R 脚本导出数据
当程序终止时,整个数据都会丢失。即使程序终止,存储在文件中也会保留一个人的数据。如果必须输入大量数据,则将它们全部输入将花费大量时间。但是,如果一个人有一个包含所有数据的文件,他/她可以使用 R 中的一些命令轻松访问文件的内容。一个人可以轻松地将他的数据从一台计算机移动到另一台计算机,而无需进行任何更改。因此,这些文件可以以各种格式存储。它可以存储在 .txt(制表符分隔值)文件中,或以表格格式存储,即 .csv(逗号分隔值)文件,也可以存储在 Internet 或云上。 R 提供了非常简单的方法来将数据导出到这些文件。
将数据导出到文本文件
存储文件的重要格式之一是文本文件。 R 提供了各种可以将数据导出到文本文件的方法。
-
write.table()
:R 基函数write.table()
可用于将数据框或矩阵导出到文本文件。Syntax:
write.table(x, file, append = FALSE, sep = ” “, dec = “.”, row.names = TRUE, col.names = TRUE)Parameters:
x: a matrix or a data frame to be written.
file: a character specifying the name of the result file.
sep: the field separator string, e.g., sep = “\t” (for tab-separated value).
dec: the string to be used as decimal separator. Default is “.”
row.names: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written.
col.names: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written.例子:
# R program to illustrate # Exporting data from R # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame to a text file using write.table() write.table(df, file = "myDataFrame.txt", sep = "\t", row.names = TRUE, col.names = NA)
输出:
-
write_tsv()
:此方法还用于通过使用readr
包的帮助将数据导出到制表符分隔(“\t”)值。Syntax: write_tsv(file, path)
Parameters:
file: a data frame to be written
path: the path to the result file例子:
# R program to illustrate # Exporting data from R # Importing readr library library(readr) # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame using write_tsv() write_tsv(df, path = "MyDataFrame.txt")
输出:
将数据导出到 csv 文件
存储文件的另一种流行格式是 csv(逗号分隔值)格式。 R 提供了各种可以将数据导出到 csv 文件的方法。
-
write.table()
:R 基函数write.table()
也可用于将数据框或矩阵导出到 csv 文件。Syntax:
write.table(x, file, append = FALSE, sep = ” “, dec = “.”, row.names = TRUE, col.names = TRUE)Parameters:
x: a matrix or a data frame to be written.
file: a character specifying the name of the result file.
sep: the field separator string, e.g., sep = “\t” (for tab-separated value).
dec: the string to be used as decimal separator. Default is “.”
row.names: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written.
col.names: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written.例子:
# R program to illustrate # Exporting data from R # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame to a text file using write.table() write.table(df, file = "myDataFrame.csv", sep = "\t", row.names = FALSE, )
输出:
-
write.csv()
:推荐使用此方法将数据导出到 csv 文件。它用 ”。”小数点和逗号(“,”)作为分隔符。例子:
# R program to illustrate # Exporting data from R # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame to a text file using write.csv() write.csv(df, file = "my_data.csv")
输出:
-
write.csv2()
:此方法与write.csv()
非常相似,但它使用逗号(“,”)作为小数点,使用分号(“;”)作为分隔符。例子:
# R program to illustrate # Exporting data from R # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame to a text file using write.csv2() write.csv2(df, file = "my_data.csv")
输出:
-
write_csv()
:此方法还用于通过使用 readr 包的帮助将数据导出为逗号分隔的(“,”)值。Syntax: write_csv(file, path)
Parameters:
file: a data frame to be written
path: the path to the result file例子:
# R program to illustrate # Exporting data from R # Importing readr library library(readr) # Creating a dataframe df = data.frame( "Name" = c("Amiya", "Raj", "Asish"), "Language" = c("R", "Python", "Java"), "Age" = c(22, 25, 45) ) # Export a data frame using write_csv() write_csv(df, path = "MyDataFrame.csv")
输出: