R: Write file (input and output)

Source: Internet
Author: User
Tags eol numeric


Since the R language is primarily used for statistical functions, reading files may be more common than writing files, but writing files is sometimes necessary.

The use of the write.table () function is very similar to read.table () except that it writes the data frame to the file instead of reading it from the file. Parameters and Options:

Write.table (x, File = "", append = FALSE, quote = TRUE, Sep = "", EOL = "\ n", na = "na", Dec = ".", Row.names = TRUE,COL.N Ames = TRUE, Qmethod = C ("Escape", "double"))

Option values and descriptions for function write.table ()

Option value

Description

X

The name of the object to write

File

File name (the object is "written" on the screen by default)

Append

If true, data that may already exist in the destination file is not deleted when the data is written, in a way that is added later

Quote

A logical or numeric vector: If true, the character variables and factors are written in double quotation marks (""), and if quote is a numeric vector, the column labels of those columns that will be written in ("")

Sep

field separators in the file

Eol

Use the last character in each line ("\ n" indicates carriage return)

Na

A character that represents true data

Dec

The character used to represent the decimal point

Row.names

A logical value that determines whether the row name is written to a file or specifies a character vector to write to the file as a row name

Col.names

A logical value (determines whether the column name is written to the file), or a character vector to be written as a column name in the file

Qmethod

If quote=true, this parameter is used to specify how the double quotation marks ("") in a character variable are handled, and if the value of the argument is "escape" (or "E", default) each "is replaced with \", and if the value is "D", each "is replaced with" "

> Kids <-C ("Jack", "Jill", "Kate")

> Ages <-C (25, 36, 18)

> D <-data.frame (kids, ages, Stringsasfactors=false)

> write.table (d, "D:/rcodes/dfile.txt")

The Dfile file contains the following content:


If you want to write the matrix to a file, you only need to declare the column name and row name.

> x <-Matrix (Scan ("D:/rcodes/readm.txt", Quiet=true), nrow=5, Byrow=true)

> write.table (x, "D:/rcodes/writem.txt", Row.names=false, Col.names=false)

The cat () function can also be used to write to a file and write one part at a time.

Cat ("abc\n", file= "D:/rcodes/writecat.txt")

Cat ("def\n", file= "D:/rcodes/writecat.txt", Append=true)

Cat (file= "WriteCAT.txt", 1, 2, "xyz\n")

The first time you call Cat (), the file WriteCAT.txt is created under the specified directory, containing a line of "ABC". The second call appends the second line, which is automatically saved after each operation. The cat () function can write multiple fields, and the last line of code in the above generates a file writeCAT.txt that has only one line of content.

You can also use the Writelines () function, which is the relative of ReadLines (). If you are using a connection, you must set the parameter "W" to indicate that you want to write the file rather than read it.

C <-File ("File", "W")

Writelines (C ("abc", "De", "F"), C)

Close (c) #这里需要主动关闭文件


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.