Reprinted from: http://www.cnblogs.com/emanlee/archive/2012/12/04/2802352.html
r language data storage and reading
1 First get current directory with getwd(), set current directory with setwd("C:/data")
2 Data Saving
Create Data Frame 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) # space Delimited
>write.table(d, file = "C:/data/foo.txt", row.names = f, quote = f, sep= "\ t") # tab-delimited file
2.2 Save As comma-delimited text
>write.csv(d, file = "C:/data/foo.csv", row.names = f, quote = f)
2.3 Save As R format file
>Save(d, File = "C:/data/foo. Rdata ")
2.4 Saving a workspace image
>save.image () = Save (List =ls (all=true), file= ". RData ")
3 Data Read
The main reading functions are:read.table(), scan(), READ.FWF (),readlines().
3.1 read "C:\Data" with read.table () Houses.dat
>SETWD ("C:/data"); Houseprice <-read.table (file= "Houses.dat")
If the first row of data is clear, the header option is used
>houseprice <-read.table ("Houses.dat", Header=true)
Read.table () variants are: Read.csv (), Read.csv2 (), Read.delim (), read.delim2 (). The first two read the comma-delimited data, and the latter two read the other delimiter data.
3.2 More flexible with scan () than read.table ().
But to specify the variable type: For example: C:\data\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
such as: 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 Excel format data read
4.1 Using the shear plate
Select Excel data, and then copy it with (Ctrl + C). Type the command in R:
>mydata <-Read.delim ("clipboard")
4.2 Use package RODBC.
such as: 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)
to an Excel Spreadsheet saved as an Excel file:
Library (xlsx) # Note: Packages need to be installed
Write.xlsx (MyData, "c:/mydata.xlsx") # Reference: https://danganothererror.wordpress.com/2012/02/12/ write-data-frame-to-excel-file/
The Writexls function from the Writexls package (link:http://cran.r-project.org/web/packages/writexls/index.html) can Write data to Excel.
Alternatively, write.xlsx from the xlsx package (link:http://cran.r-project.org/web/packages/xlsx/) would also work.
Note:
1 Writelines will add a line break at the end of the last line/or at the bottom of each line
# Fileconn<-file (Output_fasta)
# Writelines (mystr, Fileconn)
# Close (fileconn)
2 Another way to write a file is sink, and does not add line breaks at the end of the row
Sink (Output_fasta)
Cat (MYSTR)
Sink ()
Write is a wrapper for cat, which gives further details on the format used.
Save for writing any R objects, write.table for data frames, and scan for reading data.
REF:
Http://www.statmethods.net/input/exportingdata.html
http://hi.baidu.com/wuyu466/item/d46edcd96c2838e955347f2c