R language CSV and txt text read-in distinction (Sep parameter)

Source: Internet
Author: User

R language CSV and txt text read-in distinction

The R language is convenient for processing data, and the first step in processing data is to read the data into memory space, usually the most commonly used text data storage format has two kinds;

One is CSV (comma delimiter text) and the other is txt (tab delimiter or space separator), sometimes reading these two file formats is easy to confuse.

1, when we read the data, generally write the file name in two ways:

(1) Set the directory where the data is stored as the working directory (SETWD ("File path"), just write the file name when you read the files

1 setwd ('c:/data/mydata')2read. Table (' Cancer.txt ', header=ture)

(2) Do not set working directory, read into the file using the full path (including absolute and relative path)

1 read. Table ('c:/data/mydata/cancer.txt', header=ture)

(3) I prefer the first one, because after setting up the directory I know exactly what directory I am in, and that does not mean that the second type is meaningless;

When we started out in a system environment and switched to an exact directory (both DOS and Linux use CD commands), the second method is also used in this case

Just write the file name. All in all, it is necessary to switch directories, except that you are handing over to the system (switching with the CD command), and the SETWD function specifies the exact path to complete.

2, read in using the Read.table function

(1) Read into TXT file

1 read. Table ('cancer.txt', header=true)

Header=true represents the first row as the column name when reading the data (if False, instead of using the first row in the file as the column name), you can also ask Header=t (or header=f)

The SEP parameter is not specified because the Read.txt function default parameter sep= ' \ t '. You can also specify if you like, and that would be a bit superfluous.

(2) read into CSV file

1 read. txt ('cancer.csv', header=ture, sep=',')

You must specify sep= ', ' do not specify no error but there is only one column of data you read in

3, read in using the Read.csv function

(1) Read into TXT file

1 read. csv ('cancer.txt', header=ture, sep='\ t')

Sep parameter must be specified

(2) read into CSV file

1 read. csv ('cancer.csv', header=ture)

Do not force the SEP parameter to be specified because the default sep= ', '

4, I recommend using the Read.table function to read the TXT file, the Read.csv function read into the CSV file; You can write a few words less why not steal a little lazy?

R language CSV and txt text read-in distinction (Sep parameter)

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.