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

  • Published on
    13-Nov-2014

  • View
    887

  • Download
    2

Embed Size (px)

Transcript

<p>2</p> <p> . , .</p> <p>2.1. R R. , , R , , . : , . . , , , (Emacs/Vi ). , , , , . , , . (, ), . , R , . R : &gt; getwd () [1] " / home / username / "</p> <p>2.1. R </p> <p>19</p> <p> , . : &gt; setwd ( " . / workdir " ) &gt; getwd () [1] " / home / username / workdir " , help(getwd). , : &gt; dir () [1] " mydata . txt " . read.table(): &gt; 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(): &gt; file . show ( " mydata . txt " ) ;; 1;2;3 4;5;6 7;8;9 R , read.table(), . , sep "". , , , sep . , , , . , , : 1) , :</p> <p>2.1. R </p> <p>20</p> <p>&gt; read . table ( " .. / workdir / mydata . txt " ) 2) , . ru_RU.UTF-8, KOI8-R, file(): &gt; 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 . , ( ): &gt; file . show ( " mydata2 . txt " ) 1 2 3 4 5 6 7 8 9 &gt; read . table ( " mydata2 . txt " , head = TRUE ) 1 2 3 4 5 6 7 8 9 4) , . - , dec: &gt; 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.</p> <p>2.1. R </p> <p>21</p> <p> , . , , R, - ( Perl). . , , , , , MS Excel. , : R? : , . , . , . R . R foreign, , Minitab, S, SAS, SPSS, Stata, Systat, DBF. , : &gt; library ( foreign ) &gt; help ( package = foreign ) Excel, . , R , . MS Excel 2007 . R . OpenOce Calc xls- , , R: &gt; read . table ( " clipboard " ) , , Excel- . : - . , / , . :</p> <p> R! : 1) R . . pixmap. R ArcInfo . ( maps, maptools) .</p> <p>2.2. </p> <p>22</p> <p>2) R . , . &gt; x save (x , file = " x . rd " ) &gt; rm ( x ) &gt; x : " x " &gt; dir () [1] " x . rd " &gt; load ( " x . rd " ) &gt; x [1] " " save() load(), X11 ( fonts = c ( + " -rfx - helvetica -% s -% s - * -* -%d - * -* -* -* -* - koi8 - r " ,</p> <p>2.2. </p> <p>23</p> <p>. 2.1. X11 + " - adobe - symbol - medium -r - * -* -%d - * -* -* -* -* -* -* " ))</p> <p> fonts X11 , c(). , , , , . xfontsel . Fonts , help(X11). : &gt; plot (1:20 , main = " " ) &gt; legend ( " topleft " , pch =1 , legend = " " ) , . , , . , R. : plot() , ( generic , ). , , , . , 1:20 1 20, , , ( ), .</p> <p>2.2. </p> <p>24</p> <p> - , , , . : &gt; plot ( cars ) &gt; title ( main = " 20 - " ) , . , plot(), , title(). cars 1 R , , . , , : speed distance ( ). plot() , , scatterplot, X (), Y , . , plot(), , , trees . , , , data() ( , ).</p> <p>2.2.2. plot(), R , , 2 . , , R . : &gt; dev . off () R , , , , . R , ( Mac OS X ). , . . ( - ), . : &gt; png ( file = " 1 -20. png " , bg = " transparent " ) &gt; plot (1:20) &gt; dev . off () png() , , (, ,1 , 2</p> <p> cars , , ("?cars"). X Window X11.</p> <p>2.2. </p> <p>25</p> <p> Web). . dev.off(), 1-20.png. png() . , , . jpeg(), jpeg-. R , , PDF. , , . . PDF-, : &gt; &gt; &gt; &gt; pdf ( " 1 -20. pdf " , family = " NimbusSan " , encoding = " KOI8 - R. enc " ) plot (1:20 , main = " " ) dev . off () embedFonts ( " 1 -20. pdf " )</p> <p> , , , , . , R: CP1251.enc KOI8-U.enc. embedFonts(). , NimbusSan R Ghostscript, , . PDF, R , , PostScript, xg picTeX. RSvgDevice, SVG. , , Inkscape.</p> <p>2.2.3. , R . , R. , , : . , , , : &gt; &gt; + &gt; &gt; &gt; &gt; &gt; # eps - 6 6 postscript ( " 2 hist . eps " , width =6.0 , height =6.0 , horizontal = FALSE , onefile = FALSE , paper = " special " ) # old . par par ( old . par ) &gt; dev . off () par() , mfrow, . mfrow c(1,1), . par() ( 71 ), old.par, . , hist() , .</p> <p>2.2.4. , R . , , . . . R . S-PLUS, R. , , , , : . R: &gt; dotchart ( Titanic [ , , " Adult " ," No " ] ,</p> <p>2.2. </p> <p>27</p> <p>. 2.3. +</p> <p>. 2.4. -</p> <p>main = " " )</p> <p> Titanic , , . ( ) . R 3 , . R (, rgl, OpenGL), , , R Trellis graphs -: &gt; coplot ( log ( Volume ) ~ log ( Girth ) Height, data = trees)</p> <p> ( ) . , . , -, S-PLUS.</p> <p>3 , , , Pie charts are a very bad way of displaying information .</p> <p>2.3. </p> <p>28</p> <p>2.2.5. , (, ) , . , , . , R , R , , . , : &gt; plot (1:20) &gt; text ( locator () , " " , pos =4) , , . identify(), rggobi, TeachingDemos iplot.</p> <p>2.3. R (, ) R . , . (, ), . , . , . write.table(): &gt; write . table ( file = " trees . csv " , trees , + row . names =F , sep = " ; " , quote = F ) trees.csv, R trees. , ? sink(): &gt; sink ( " 1. txt " , split = T ) &gt; 2+2 [1] 4 &gt; sink ()</p> <p>2.4. </p> <p>29</p> <p> "[1] 4 . , , , - : &gt; print ( " 2+2 " ) [1] " 2+2 " &gt; 2+2 [1] 4 . savehistory(), save.image(). , .</p> <p>2.4. , R, , A , L TEX ([1]) HTML, xtable. , , - R -. HTML , R2HTML CRAN: &gt; library ( R2HTML ) &gt; dir . create ( " example " ) &gt; HTMLStart ( " example " ) HTML &gt; 2+2 HTML &gt; plot (1:20) HTML &gt; HTMLplot () HTML &gt; HTMLStop () &gt; example HTML-, , . . , , R, , R , ? literate programming ( ) , TEX. R , R . , , A c L TEX- , , test-Sweave.Rnw:</p> <p>2.4. </p> <p>30</p> <p>\ 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 } : &gt;= 1 + 1 1 + pi sin ( pi / 2) @ : &gt;= plot (1:20) @ \ end { document } R: &gt; 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 :</p> <p>= &gt; latex test - Sweave . tex = &gt; dvips test - Sweave . dvi = &gt; gv test - Sweave . ps</p> <p>2.4. </p> <p>31</p> <p> Sweave.. 7 2008 .R : &gt;1+1 [1] 2 &gt; 1 + pi [1] 4.141593 &gt; sin(pi/2) [1] 1 : &gt; plot(1:20)</p> <p>. 2.5. Sweave # = &gt; pdflatex test - Sweave . tex = &gt; 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).</p>