R语言-《R in Action,2rd》-Robert I.Kabacoff-Chapter 1.Introduction to R

R in Action

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 class attribute 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")
1.2 设定工作环境并保存 or 菜单栏
> getwd()
[1] "C:/Users/Administrator/Documents"
> setwd("D:/Users/")
## 建议新旧工作环境分开在不同的文件夹
> load("~/hypertension.RData")  ##若未更改工作的保存地址
> load("C:/Users/Administrator/Documents/hypertension.RData")  ##若已更改文档保存位置
> options()
> options(digits = 3)
> getOption("digits")
[1] 3
> x <- runif(20)
> hist(x)
13.4 Input and output
## 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   19    7      6
  Treated    6    5     16

Arthrt> mosaic(art, gp = shading_Friendly)
Hit <Return> to see next plot: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值