函数功能:

write.table prints its required argument x 
(after converting it to a data frame if it is not one nor a matrix) 
to a file or connection.

将X输出到文件或者链接
函数语法:

write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
            eol = "\n", na = "NA", dec = ".", row.names = TRUE,
            col.names = TRUE, qmethod = c("escape", "double"),
            fileEncoding = "")

write.csv(...)
write.csv2(...)

函数参数

x	
the object to be written, preferably a matrix or data frame. 
If not, it is attempted to coerce x to a data frame.

要写入的对象,最好是矩阵或数据框。 如果不是,则尝试将x强制转换为数据框。

file	
either a character string naming a file or a connection open for writing. 
"" indicates output to the console.

表示文件名的字符串或者链接。“”表示输出到控制台

> studentID <- c(1,2,3,4,5)
> gender <- c('M','F','M','M','F')
> math <- c(40,60,70,60,90)
> English <- c(98,56,78,93,79)
> Chinese <- c(86,54,78,90,98)
> data <- data.frame(studentID,gender,math,English,Chinese,stringsAsFactors = F)
> data
  studentID gender math English Chinese
1         1      M   40      98      86
2         2      F   60      56      54
3         3      M   70      78      78
4         4      M   60      93      90
5         5      F   90      79      98
> write.csv(data,"")
"","studentID","gender","math","English","Chinese"
"1",1,"M",40,98,86
"2",2,"F",60,56,54
"3",3,"M",70,78,78
"4",4,"M",60,93,90
"5",5,"F",90,79,98

“” :输出到控制台
在这里插入图片描述

append	
logical. Only relevant if file is a character string. 
If TRUE, the output is appended to the file. 
If FALSE, any existing file of the name is destroyed.

逻辑值,当文件名为字符串时(非链接)使用有效,若取值为TRUE,输出结果会新增到原文件中,若取值为FALSE,新输出结果替换原文件。默认取值为FALSE,替换原文件
append=FALSE,默认取值,此时新输出数据将替代原数据
在这里插入图片描述
在这里插入图片描述
append=TRUE,则在原数据上新增,write.csv无法使用

These wrappers are deliberately inflexible:
 they are designed to ensure that the correct conventions are used to write a valid file. 
 Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning.

这些包装程序是故意不灵活的:
它们旨在确保使用正确的约定来写入有效文件。 尝试更改append, col.names, sep, dec or qmethod尝试将被忽略,并显示警告。
write.table函数才可以使用
在这里插入图片描述
在这里插入图片描述

quote	
a logical value (TRUE or FALSE) or a numeric vector. 
If TRUE, any character or factor columns will be surrounded by double quotes. 
If a numeric vector, its elements are taken as the indices of columns to quote. 
In both cases, row and column names are quoted if they are written. 
If FALSE, nothing is quoted.

引用
逻辑值(TRUE或FALSE)或数字向量。 如果为TRUE,则任何字符或因子列都将用双引号引起来。 如果是数字矢量,则将其元素用作要引用的列的索引。 在这两种情况下,如果都写了行名和列名,则要加引号。 如果为FALSE,则不引用任何内容。
在这里插入图片描述

sep	
the field separator string. Values within each row of x are separated by this string.

分隔符
在这里插入图片描述

eol	
the character(s) to print at the end of each line (row). 
For example, eol = "\r\n" will produce Windows' line endings on a Unix-alike OS, 
and eol = "\r" will produce files as expected by Excel:mac 2004.
na	
the string to use for missing values in the data.

用于缺失值的字符

在这里插入图片描述
在这里插入图片描述

dec	
the string to use for decimal points in numeric or complex columns: 
must be a single character.

数值或者复数型数值列中用于小数点的字符,取值必须为单个字符。

write.table(data,'C:\\Users\\***\\Desktop\\c1.txt',
            dec=',')

在这里插入图片描述

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.

行名
逻辑值,指示x的行名是否要与x一起写入,或者为要写入的行名的字符向量。

row.names=F,不带默认的行名称1,2,3,4
在这里插入图片描述
在这里插入图片描述
为输出结果定义行名称 row.names=c(‘r1’,‘r2’,‘r3’,‘r4’)
在这里插入图片描述
在这里插入图片描述

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. 
See the section on ‘CSV files’ for the meaning of col.names = NA.

列名
逻辑值,指示x的列名是否要与x一起写入,或者是要写入的列名的字符向量。

write.table(data,'C:\\Users\\***\\Desktop\\c1.txt',
            col.names=c('学号','性别','数学','英语','语文'))

在这里插入图片描述

qmethod	
a character string specifying how to deal with 
embedded double quote characters when quoting strings. 
Must be one of "escape" (default for write.table), 
in which case the quote character is escaped in C style by a backslash, 
or "double" (default for write.csv and write.csv2), 
in which case it is doubled. 
You can specify just the initial letter.
fileEncoding
character string: if non-empty declares the encoding to be used on a file (not a connection) so the character data can be re-encoded as they are written. 

输出文件编码
字符串:表明输出字符串中的内容编码方式

参数参数含义write.tablewrite.csv
x要输出的数据
file文件路径
append是否将输出追加到原文件×,不能修改参数为T
quote对引号的处理
sep行中字段之间的分隔符×,csv以逗号分隔,不能修改参数
eol尚不清楚
na缺失值
dec小数点×, csv使用点 . 表示小数点
row.names行名称
col.names列名称×,不能修改
qmethod尚不清楚×,不能修改
fileEncoding输出文件编码

Data Output write.table

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐