R: Read and write files (input and output)

Source: Internet
Author: User


► Reading a data frame or matrix from a file

You can use Read.table () to read the data frame.

> Z <-read.table ("D:/rcodes/zfile.txt", Header=true)

> Z

Name Age1

John 252

Mary 283

Jim 19

Note: the scan () does not read the data frame correctly because the values and characters in this file are mixed and contain the header.

There seems to be no way to read a data frame directly from a file, but with other tools it is easy to do so. A quick and easy way to do this is to use scan () to read rows by line, using the Byrow option in matrix () to set the matrix to be stored by row rather than by column.

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

> x

[, 1] [, 2] [, 3] [, 4]

[1,] 1 0 1 0

[2,] 0 1 0 1

[3,] 1 1 0 1

[4,] 1 0 0 1

[5,] 0 0 1 0

The above approach is effective for a one-time fast operation, but there is also a way to use read.table () to read the data frame first and then convert it to a matrix using As.matrix (). This is a common approach:

> Read.matrix <-function (filename) {

+ As.matrix (read.table (filename))

+ }

> Read.matrix ("D:/rcodes/zfile.txt")

V1 V2 V3 V4

[1,] 1 0 1 0

[2,] 0 1 0 1

[3,] 1 1 0 1

[4,] 1 0 0 1

[5,] 0 0 1 0

► Read text Files

In computer literature, there is usually a difference between a text file and a binary file (binary files). But this difference is somewhat misleading, in fact, each computer file is composed of 0 and 1 encoding, in this sense, all files can be considered binary files. The text file is defined here as a file consisting primarily of ASCII characters or other human language encodings (such as GB encoding for Chinese), and the use of newline characters makes it feel that the file is made up of rows.

A non-text file, such as a JPEG image or executable program file, is often referred to as a "binary file."

You can use ReadLines () to read a text file, either one line at a time or all at once.

All-in-one read sampling example:

> Z <-readlines ("D:/rcodes/zfile.txt")

> Z

[1] "John MN" "Mary NB" "Jim The JF"

► Introduction of the Connection

"Connection (Connection)" is a basic mechanism for various I/O (input and output) operations in R. It can be used to read files, and connections are typically created by calling file (), url (), or other R functions. can be passed ". Connection "command to see a list of these functions.

We can read the file row by line, as in the following example:

> rs <-file ("D:/rcodes/zfile.txt", "R")

> ReadLines (RS, n=1)

[1] "John MN"

> ReadLines (RS, n=1)

[1] "Mary NB"

> ReadLines (RS, n=1)

[1] "Jim the JF"

> ReadLines (RS, n=1)

Character (0)

Open the connection, assign the result to the variable RS, and set the parameter n=1 to allow the program to read only one line of the file at a time. When R encounters a file terminator (EOF), it returns a control. We need to set up a connection to let R trace the process of the file.

> rs <-file ("D:/rcodes/zfile.txt", "R")

> while (TRUE) {

+ rst <-readlines (RS, n=1)

+ if (length (rst) = = 0) {

+ Print ("reached the end")

+ break+}

+ else{

+ Print (RST)

+     }

+ }

[1] "John MN"

[1] "Mary NB"

[1] "Jim the JF"

[1] "reached the end"

If you want to "rewind" and re-read from the beginning of the file, you can use the Seek () function:

> rs <-file ("D:/rcodes/zfile.txt", "R")

> ReadLines (RS, n=2)

[1] "John MN" "Mary NB"

> Seek (Con=rs, where=0)

[1] 24

> ReadLines (RS, n=1)

[1] "John MN"

The where=0 parameter in Seek () indicates that the starting pointer points to the beginning of the file, which is read directly from the beginning. The return value of this command is 24, indicating that the pointer is located at 24 before executing the command.

You can use the close () function to close a connection. Closing the connection allows the system to know that the required file content has been fully read and can now be formally tilted to the earth disk.


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.