Data analysis and graphics with R - SECOND EDITION
ROBERT I. KABACOFF
ISBN: 9781617291388
## An "object" is basically anything that can be assigned a value.
For R, that is just about "everything"(data, functions, graphs, analytic
results, and more).
Every object has a classattribute telling R how to handle it.
Once you’ve had a chance to try out R’s graphics capabilities, it’s time to get back to the business of analyzing data. Data rarely comes in a readily usable format. Significant time must often be spent combining data from different sources, cleaning messy data (miscoded data, mismatched data, missing data), and creating new variables (combined variables, transformed variables, recoded variables) before the questions of interest can be addressed.
Today’s data analysts need to access data from a wide range of sources (database management systems, text files, statistical packages, and spreadsheets), merge the pieces of data together, clean and annotate them, analyze them with the latest methods, present the findings in meaningful and graphically appealing ways, and incorporate the results into attractive reports that can be distributed to stakeholders and the public. As you’ll see in the following pages, R is a comprehensive software package that’s ideally suited to accomplish these goals.
Chapter 1. Introduction to R
1.1 相关性
> age <-c(1,3,5,2,11,9,3,9,12,3)> weight <-c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1)>cor(age, weight)[1]0.9075655>plot(age, weight)
## "Tips":>demo() ## 列出哪些可以举例
>demo(image()) ## 不断敲回车就可以出现各种图像的示例
> ## The vignettes returned by the vignette() function are practical
introductory articles provided in "PDF" format. Not all packages
have vignettes.>vignette()>vignette("broom")
## source("myscript.R") runs a set of R statements contained in the
file "myscript.R".>source("myscript.R")
## 1.The sink("filename") function redirects output to the file filename.
By default,if the file already exists, its contents are overwritten.2.Include the option "append=TRUE" to append text to the file rather than
overwriting it.3.Including the option "split=TRUE" will send output to both the screen and
the output file.4.Issuing the command "sink()" without options will return output to the screen alone.>sink("myoutput")>sink("myoutput", append=TRUE, split)>pdf("mygraphs.pdf")
1.4 Packages
## The function .libPaths() shows you where your library is located.>.libPaths()[1]"D:/Program Files/R Program/R-3.5.1/library"
## The function library() shows you what packages you’ve saved in your library.>library()
## The command search() tells you which packages are loaded and ready to use.>search()
## Use the command "update.packages()" to update any packages that you’ve installed.
## To see details on your packages, you can use the "installed.packages()"
command. It lists the packages you have, along with their version numbers,
dependencies, and other information.> installed.packages() ## 查看所有已经安装的detail
> install.packages() ## 安装
1.8. Packages
## 打开帮助文档
> help.start()
## 查看说明
>library(vcd)>help(package="vcd")
## 作图,"vcd"分类变量
>example("Arthritis")
Arthrt>data("Arthritis")
Arthrt> art <-xtabs(~ Treatment + Improved, data = Arthritis, subset = Sex =="Female")
Arthrt> art
Improved
Treatment None Some Marked
Placebo 1976
Treated 6516
Arthrt>mosaic(art, gp = shading_Friendly)
Hit <Return> to see next plot: