R-language data file read/write

Source: Internet
Author: User
R language data storage and reading

1. First Use getwd () to get the current directory, and use setwd ("C:/Data") to set the current directory:

2.0 Data Storage: create data box D:

> D <-data. frame (OBS = C (1, 2, 3), treat = C ("A", "B", "A"), weight = C (2.3, Na, 9 ))

2.1 save as simple text:

> Write. Table (D, file = "C:/data/foo.txt", row. Names = F, quote = f)

2.2 Save As comma-separated text:

> Write.csv (D, file = "C:/data/foo.csv", row. Names = F, quote = f)

2.3 save as an R file:

> Save (D, file = "C:/data/Foo. RDATA ")

2.4 save a workspace image:

> Save. Image () = save (list = ls (all = true), file = ". RDATA ")

3.0 Data Reading: Read functions include: Read. Table (), scan (), read. fwf ().

3.1 Use read. Table () to read houses. dat under "C: \ data:

> Setwd ("C:/Data"); houseprice <-read. Table (file = "houses. dat ")

If the first row of data is specified as the header, use the header option:

> Houseprice <-read. Table ("houses. dat", header = true)

Read. table () Deformation: aread.csv (Region, read.csv 2 (), read. delim (), read. delim2 (). the first two read the comma-separated data, and the last two read other delimiter data.

3.2 use scan () is more flexible than read. Table. However, you must specify the variable type, for example, C: \ data. DAT:

M 65 168

M 70 172

F 54 156

F 58 163

> Mydata <-scan ("data. dat", what = List ("", 0, 0 ))

> Mydata <-scan ("data. dat", what = List (sex = "", Weight = 0, Height = 0 ))

3.3 Use read. fwf () to read some fixed-width data in the file, for example, C: \ data \ data.txt:

A1.501.2

A1.551.3

B1.601.4

> Mydata <-read. fwf ("data.txt", widths = C (1, 4, 3), Col. names = C ("X", "Y", "Z "))

4.0 reading data in Excel format:

4.1 Use clipboard: Select Excel Data and copy it with Ctrl + C. In R, type the following command:

> Mydata <-read. delim ("Clipboard ")

4.2 UseProgramPackage rodbc. For example: C: \ data \ body.xls

Sex weight height

M 65 168

M 70 172

F 54 156

F 58 163

> Library (rodbc)

> Z <-odbcconnectexcel ("C:/data/body.xls ")

> Foo <-sqlfetch (Z, "sheet1 ")

> Close (z)

 

Http://hi.baidu.com/wuyu466/item/d46edcd96c2838e955347f2c

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.