R: Данные и графики

  • Published on
    13-Nov-2014

  • View
    887

  • Download
    2

Embed Size (px)

Transcript

2

. , .

2.1. R R. , , R , , . : , . . , , , (Emacs/Vi ). , , , , . , , . (, ), . , R , . R : > getwd () [1] " / home / username / "

2.1. R

19

, . : > setwd ( " . / workdir " ) > getwd () [1] " / home / username / workdir " , help(getwd). , : > dir () [1] " mydata . txt " . read.table(): > read . table ( " mydata . txt " , sep = " ; " , head = TRUE ) 1 1 2 3 2 4 5 6 3 7 8 9 , . , (head=TRUE) (sep=";"). read.table() , , . R, file.show(): > file . show ( " mydata . txt " ) ;; 1;2;3 4;5;6 7;8;9 R , read.table(), . , sep "". , , , sep . , , , . , , : 1) , :

2.1. R

20

> read . table ( " .. / workdir / mydata . txt " ) 2) , . ru_RU.UTF-8, KOI8-R, file(): > read . table ( + file ( " mydata - unicode . txt " , encoding = " KOI8 - R " ) , + sep = " ; " , head = TRUE ) 1 1 2 3 2 4 5 6 3 7 8 9 3) , R . , ( ): > file . show ( " mydata2 . txt " ) 1 2 3 4 5 6 7 8 9 > read . table ( " mydata2 . txt " , head = TRUE ) 1 2 3 4 5 6 7 8 9 4) , . - , dec: > read . table ( " mydata3 . txt " , dec = " ," , h = T ) 1.1 2.2 3.3 4.4 5.0 6.0 7.0 8.0 9.0 (h=T). , , TRUE/FALSE.

2.1. R

21

, . , , R, - ( Perl). . , , , , , MS Excel. , : R? : , . , . , . R . R foreign, , Minitab, S, SAS, SPSS, Stata, Systat, DBF. , : > library ( foreign ) > help ( package = foreign ) Excel, . , R , . MS Excel 2007 . R . OpenOce Calc xls- , , R: > read . table ( " clipboard " ) , , Excel- . : - . , / , . :

R! : 1) R . . pixmap. R ArcInfo . ( maps, maptools) .

2.2.

22

2) R . , . > x save (x , file = " x . rd " ) > rm ( x ) > x : " x " > dir () [1] " x . rd " > load ( " x . rd " ) > x [1] " " save() load(), X11 ( fonts = c ( + " -rfx - helvetica -% s -% s - * -* -%d - * -* -* -* -* - koi8 - r " ,

2.2.

23

. 2.1. X11 + " - adobe - symbol - medium -r - * -* -%d - * -* -* -* -* -* -* " ))

fonts X11 , c(). , , , , . xfontsel . Fonts , help(X11). : > plot (1:20 , main = " " ) > legend ( " topleft " , pch =1 , legend = " " ) , . , , . , R. : plot() , ( generic , ). , , , . , 1:20 1 20, , , ( ), .

2.2.

24

- , , , . : > plot ( cars ) > title ( main = " 20 - " ) , . , plot(), , title(). cars 1 R , , . , , : speed distance ( ). plot() , , scatterplot, X (), Y , . , plot(), , , trees . , , , data() ( , ).

2.2.2. plot(), R , , 2 . , , R . : > dev . off () R , , , , . R , ( Mac OS X ). , . . ( - ), . : > png ( file = " 1 -20. png " , bg = " transparent " ) > plot (1:20) > dev . off () png() , , (, ,1 , 2

cars , , ("?cars"). X Window X11.

2.2.

25

Web). . dev.off(), 1-20.png. png() . , , . jpeg(), jpeg-. R , , PDF. , , . . PDF-, : > > > > pdf ( " 1 -20. pdf " , family = " NimbusSan " , encoding = " KOI8 - R. enc " ) plot (1:20 , main = " " ) dev . off () embedFonts ( " 1 -20. pdf " )

, , , , . , R: CP1251.enc KOI8-U.enc. embedFonts(). , NimbusSan R Ghostscript, , . PDF, R , , PostScript, xg picTeX. RSvgDevice, SVG. , , Inkscape.

2.2.3. , R . , R. , , : . , , , : > > + > > > > > # eps - 6 6 postscript ( " 2 hist . eps " , width =6.0 , height =6.0 , horizontal = FALSE , onefile = FALSE , paper = " special " ) # old . par par ( old . par ) > dev . off () par() , mfrow, . mfrow c(1,1), . par() ( 71 ), old.par, . , hist() , .

2.2.4. , R . , , . . . R . S-PLUS, R. , , , , : . R: > dotchart ( Titanic [ , , " Adult " ," No " ] ,

2.2.

27

. 2.3. +

. 2.4. -

main = " " )

Titanic , , . ( ) . R 3 , . R (, rgl, OpenGL), , , R Trellis graphs -: > coplot ( log ( Volume ) ~ log ( Girth ) Height, data = trees)

( ) . , . , -, S-PLUS.

3 , , , Pie charts are a very bad way of displaying information .

2.3.

28

2.2.5. , (, ) , . , , . , R , R , , . , : > plot (1:20) > text ( locator () , " " , pos =4) , , . identify(), rggobi, TeachingDemos iplot.

2.3. R (, ) R . , . (, ), . , . , . write.table(): > write . table ( file = " trees . csv " , trees , + row . names =F , sep = " ; " , quote = F ) trees.csv, R trees. , ? sink(): > sink ( " 1. txt " , split = T ) > 2+2 [1] 4 > sink ()

2.4.

29

"[1] 4 . , , , - : > print ( " 2+2 " ) [1] " 2+2 " > 2+2 [1] 4 . savehistory(), save.image(). , .

2.4. , R, , A , L TEX ([1]) HTML, xtable. , , - R -. HTML , R2HTML CRAN: > library ( R2HTML ) > dir . create ( " example " ) > HTMLStart ( " example " ) HTML > 2+2 HTML > plot (1:20) HTML > HTMLplot () HTML > HTMLStop () > example HTML-, , . . , , R, , R , ? literate programming ( ) , TEX. R , R . , , A c L TEX- , , test-Sweave.Rnw:

2.4.

30

\ documentclass [ a 4 paper ,12 pt ]{ scrartcl } % \ LaTeX - \ usepackage [ T 2 A ]{ fontenc } % utf 8 % cp 1251 koi 8 - r \ usepackage [ utf 8]{ inputenc } \ usepackage [ english , russian ]{ babel } \ usepackage { indentfirst } \ title { Sweave } \ author { . .\ , } \ begin { document } % \ maketitle \ textsf { R } : >= 1 + 1 1 + pi sin ( pi / 2) @ : >= plot (1:20) @ \ end { document } R: > Sweave ( " test - Sweave . Rnw " ) Writing to file test - Sweave . tex Processing code chunks ... 1 : echo print term verbatim 2 : echo term verbatim eps pdf You can now run LaTeX on test - Sweave . tex A L TEX- test-Sweave.tex. , , latex/dvips pdatex :

= > latex test - Sweave . tex = > dvips test - Sweave . dvi = > gv test - Sweave . ps

2.4.

31

Sweave.. 7 2008 .R : >1+1 [1] 2 > 1 + pi [1] 4.141593 > sin(pi/2) [1] 1 : > plot(1:20)

. 2.5. Sweave # = > pdflatex test - Sweave . tex = > acroread test - Sweave . pdf , , , . , A R , L TEX- Rnw : \ usepackage [ noae ]{ Sweave } (Friedrich Leisch) : http://www.ci.tuwien.ac.at/~leisch/Sweave/. , , R2HTML HTML. brew, (, ), odfWeave, ODF ( OpenOce.org).

Recommended

View more >