《R語言實戰》學習筆記

2017年夏天開始學習R語言。

第1章 R語言介紹

1.1 R的獲取和安裝

下載Rgui:http://cran.r-project.org
可以通過安裝包(package)的可選模塊來增強R的功能。
下載RStudio:https://www.rstudio.com/products/rstudio/download/#download

1.3 R的使用

  • R是一種區分大小寫的解釋性語言。
  • R使用<-作為賦值符號,而不是傳統的=。
  • 注釋由#開頭。
  • R不提供多行注釋功能,可以用if(FALSE){}存放被忽略的代碼。
  • R會自動拓展數據結構以容納新值。
  • R的下標從1開始。
  • 變量無法被聲明,它們在首次賦值時生成。
  • Google's R Style Guide: https://google.github.io/styleguide/Rguide.xml
  • 搜索“來自Google的R語言編碼風格指南”可以找到這份文檔的中文版
  • R programming for those coming from other languages: https://www.johndcook.com/R_language_for_programmers.html

1.3.1 獲取幫助 P10

  • help.start() 打開幫助文檔首頁

1.3.2 工作空間 P11

  • getwd() 查看當前工作目錄
  • setwd() 設定當前工作目錄
  • dir.create() 創建新目錄
  • 路徑使用正斜桿/
  • 命令的歷史紀錄保存到文件.Rhistory
  • 工作空間保存到當前目錄中的文件.RData
  • q() 退出R

1.3.3 輸入和輸出

  • 輸入:source("filename")
  • 文本輸出:sink("filename")
    參數append = TRUE追加文本而非覆蓋
    參數split = TRUE將輸出同時發送到屏幕和輸出文件中
    無參數調用sink() 僅向屏幕返回輸出結果
  • 圖形輸出 P12

1.5 批處理 P15

1.6 將輸出用為輸入:結果的重用 P16

lmfit <- lm(mpg~wt, date = mtcars)

第2章 創建數據集

2.1 數據集的概念

數據構成的矩形數組,行表示觀測,列表示變量。

2.2 數據結構

2.2.1 向量

向量是用于存儲數值型、字符型或邏輯型數據的一維數組。
創建向量:c()

  • 單個向量中的數據必須擁有相同的類型或模式(數值、字符、邏輯)
  • 標量是只含一個元素的向量,用于保存常量
  • 訪問向量中的元素:
> a <- c("k", "j", "h", "a", "b", "m")
> a[3]
[1] "h"
> a[c(1,3,5)]
[1] "k" "h" "b"
> a[1,3,5]
Error in a[1, 3, 5] : 量度數目不對
> a[2:6]
[1] "j" "h" "a" "b" "m"

2.2.2 矩陣 P22

矩陣是一個二維數組,每個元素擁有相同的模式。
創建矩陣:matrix(矩陣元素, nrow = 行數, ncol = 列數, byrow = FALSE 默認按列填充<可選>, dimnames = list(行名, 列名)<可選>)

> y <- matrix(1:20, nrow = 5, ncol = 4)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20
> cells <- c(1, 26, 24, 68)
> rnames <- c("R1", "R2")
> cnames <- c("C1", "C2")
> mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = TRUE, dimnames = list(rnames, cnames))
> mymatrix
   C1 C2
R1  1 26
R2 24 68

矩陣下標的使用:
X[i, ] 矩陣X中的第i行
X[, j] 矩陣X中的第j列
X[i, j] 矩陣X中的第i行第j個元素
選擇多行或多列,下標i和j可為數值型向量。

> x <- matrix(1:10, nrow = 2)
> x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10
> x[2, ]
[1]  2  4  6  8 10
> x[, 2]
[1] 3 4
> x[1, 4]
[1] 7
> x[1, c(4,5)]
[1] 7 9

2.2.3 數組

數組和矩陣類似,但是維度可以大于2。
創建數組:array(vector, dimensions, dimnames<可選>)

> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2,3,4), dimnames = list(dim1, dim2, dim3))
> z
, , C1

   B1 B2 B3
A1  1  3  5
A2  2  4  6

, , C2

   B1 B2 B3
A1  7  9 11
A2  8 10 12

, , C3

   B1 B2 B3
A1 13 15 17
A2 14 16 18

, , C4

   B1 B2 B3
A1 19 21 23
A2 20 22 24

2.2.4 數據框

創建數據框:data.frame(col1, col2, col3, ...)

> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> patientdata <- data.frame(patientID, age, diabetes, status)
> patientdata
  patientID age diabetes    status
1         1  25    Type1      Poor
2         2  34    Type2  Improved
3         3  28    Type1 Excellent
4         4  52    Type1      Poor

選取數據框中的元素:

> patientdata[1:2]
  patientID age
1         1  25
2         2  34
3         3  28
4         4  52
> patientdata[c("diabetes", "status")]
  diabetes    status
1    Type1      Poor
2    Type2  Improved
3    Type1 Excellent
4    Type1      Poor
> patientdata$age
[1] 25 34 28 52
> table(patientdata$diabetes, patientdata$status)
       
        Excellent Improved Poor
  Type1         1        0    2
  Type2         0        1    0
  • attach()、detach()和with()
    函數attach() 可將數據框添加到R的搜索路徑中
summary(mtcars$mpg)
plot(mtcars$mpg, mtcars$disp)
plot(mtcars$mpg, mtcars$wt)
可寫成
attach(mtcars)
    summary(mpg)
    plot(mpg, disp)
     plot(mpg, wt)
detach(mtcars)

函數detach() 將數據框從搜索路徑中移除。(可省略,為保持良好的編程習慣還是要寫。)
環境中有重名對象時使用函數with() :

with(mtcars,{
    print(summary(mpg))
    plot(mpg, disp)
    plot(mpg, wt)
})
若要創建在with() 結構意外的對象,使用特殊賦值符<<-代替<-
with(mtcars, {
    keepstats <<- summary(mpg)
})
  • 實例標識符
    實例標識符可通過數據框操作函數中的rowname選項指定。
patientdata <- data.frame(patientID, age, diabetes, status, row.names = patientID)

2.2.5 因子

變量可歸結為名義型、有序型或連續型變量。

  • 名義型變量沒有順序之分。如:Diabetes(Type1、Type2)
  • 有序型變量表示一種順序關系而非數量關系。如:Status(poor、improved、excellent)
  • 連續型變量呈現為某個范圍內的任意值。如:Age
    因子:類別(名義型)變量和有序類別(有序型)變量。
    對于名義型變量:
diabetes <- c("Type1", "Type2", "Type1", "Type1")
語句diabetes <- factor(diabetes)將此向量存儲為(1, 2, 1, 1)
并在內部將其關聯為1 = Type1和2 = Type2(具體賦值根據字母順序而定)

對于有序型變量,需要為函數factor() 指定參數ordered = TRUE:

satatus <- c("Poor", "Improved", "Excellent", "Poor")
語句status <- factor(status, ordered = TRUE)會將向量編碼為(3, 2, 1, 3)
默認為字母順序排序,若要指定排序:
status <- factor(status, order = TRUE, levels = c("Poor", "Improved", "Excellent"))

對于數值型變量:

如果男性編碼成1,女性編碼成2:
sex <- factor(sex, levels = c(1, 2), labels = c("Male", "Female"))
標簽"Male" 和"Female” 將代替1和2在結果中輸出

因子的使用:

> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <-  c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> diabetes <- factor(diabetes)
> status <- factor(status, ordered = TRUE)
> patientdata <- data.frame(patientID, age, diabetes, status)
> str(patientdata) #顯示數據框內部編碼結構
'data.frame':   4 obs. of  4 variables:
 $ patientID: num  1 2 3 4
 $ age      : num  25 34 28 52
 $ diabetes : Factor w/ 2 levels "Type1","Type2": 1 2 1 1
 $ status   : Ord.factor w/ 3 levels "Excellent"<"Improved"<..: 3 2 1 3
> summary(patientdata) #顯示對象的統計概要(區別對待各個變量)
   patientID         age         diabetes       status 
 Min.   :1.00   Min.   :25.00   Type1:3   Excellent:1  
 1st Qu.:1.75   1st Qu.:27.25   Type2:1   Improved :1  
 Median :2.50   Median :31.00             Poor     :2  
 Mean   :2.50   Mean   :34.75                          
 3rd Qu.:3.25   3rd Qu.:38.50                          
 Max.   :4.00   Max.   :52.00    

2.2.5 列表

列表就是一些對象的有序集合
創建列表:
mylist <- list(object1, objext2, ...)
mylist <- list(name1 = object1, name2 = object2, ...)

> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow = 5)
> k <- c("one", "two", "three")
> mylist <- list(title = g, ages = h, j, k)
> mylist
$title
[1] "My First List"

$ages
[1] 25 26 18 39

[[3]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

[[4]]
[1] "one"   "two"   "three"

> mylist[[2]]
[1] 25 26 18 39
> mylist[["ages"]]
[1] 25 26 18 39

2.3 數據的輸入

2.3.1使用鍵盤輸入數據

  1. R內置文本編輯器
> mydata <- data.frame(age = numeric(0), gender = character(0), weight = numeric(0))
> mydata <- edit(mydata)
#語句mydata <- edit(mydata) 可以簡便寫為fix(mydata)
  1. 代碼中直接嵌入數據
> mydatatxt <- "
+ age gender weight
+ 25 m 166
+ 30 f 115
+ 18 f 120
+ "
> mydatatxt <- read.table(header = TRUE, text = mydatatxt)

2.3.2 從帶分隔符的文本導入數據 P33

mydataframe <- read.table(file, options)
其中file是一個帶分隔符的ASCII文本文件,options是控制如何處理數據的選項。

2.3.3 導入Excel數據

  • 最好方式:在Excel中導出為逗號分隔文件csv,再用前文描述導入R。
  • 直接導入:xlsx包、xlsxjars和rJava包。P35

2.3.4 導入XML數據

www.omegahat.org/RSXML

2.3.5 從網頁抓取數據

2.3.6 導入SPSS數據 P36

2.3.7 導入SAS數據 P37

2.3.8 導入Stata數據

library(foreign)
mydataframe <- read.dta("mydata.dta")

2.3.9 導入NetCDF數據 P38

2.3.10 導入HDF5數據 p38

2.3.11 訪問數據庫管理系統 P38

2.3.12 通過Stat/ Transfer導入數據

www.stattransfer.com 數據轉換應用程序。

2.4 數據集的標注

2.4.1 變量標簽

就是重命名:

names(patientdata)[2] <- "Age at hospitalization (in years)"

2.4.2 值標簽

如果男性編碼成1,女性編碼成2:
sex <- factor(sex, levels = c(1, 2), labels = c("Male", "Female"))
標簽"Male" 和"Female” 將代替1和2在結果中輸出

2.5 處理數據對象的實用函數

P41

第3章 圖形初階

3.1 使用圖形

保存圖形:代碼、用戶圖形界面保存。P44

3.3 圖形參數

修改圖形的特征。

> opar <- par(no.readonly = TRUE)
> par(lty = 2, pch = 17)
> par(opar)

3.3.1 符號和線條

  • pch 指定繪制點時只用的符號
  • cex 指定符號的大小
  • lty 指定線條的類型
  • lwd 指定線條的寬度
> plot(dose, drugA, type = "b", lty = 3, lwd = 3, pch = 15, cex = 2)

3.3.2 顏色

#RColorBrewer創建吸引人的顏色配對
> install.packages("RColorBrewer")
> library(RColorBrewer)
> n <- 7
> mycolors <- brewer.pal(n,"Set1")
> barplot(rep(1, n), col = mycolors)
#彩虹漸變色
> n <- 10
> mycolors <- rainbow(n)
> pie(rep(1, n), labels = mycolors, col = mycolors)
#10階灰度色
> mygrays <- gray(0:n/n)
> pie(rep(1, n), labels = mygrays, col = mygrays)

3.3.3 文本屬性 P50

3.3.4 圖形尺寸與邊界尺寸

使用圖形參數控制圖形外觀:

> dose <- c(20, 30, 40, 45, 60)
> drugA <- c(16, 20, 27, 40, 60)
> drugB <- c(15, 18, 25, 31, 40)
> opar <- par(no.readonly = TRUE)
> par(pin = c(2, 3))
> par(lwd = 2, cex = 1.5)
> par(cex.axis = .75, font.axis = 3)
> plot(dose, drugA, type = "b", pch = 19, lty = 2, col = "red")
> plot(dose, drugB, type = "b", pch = 23, lty = 6, col = "blue", bg = "green")
> par(opar)

3.4 添加文本、自定義坐標軸和圖例

3.4.1 標題

title(main = "main title", col.main = "red", sub = "subtitle", col.sub = "blue", xlab = "x-axis laber", tlab = "y-axis laber", col.lab = "green", cex.lab = 0.75)

3.4.2 坐標軸 P54

axis(side, at = , labers = , pos, lty = , col = , las = , tck = , ... )
> x <- c(1:10)
> y <- x
> z <- 10 / x
> opar <- par(no.readonly = TRUE)
> par(mar = c(5, 4, 4, 8) + 0.1)
> plot(x, y, type = "b", pch = 21, col = "red", yaxt = "n", lty = 3, ann = FALSE)
> lines(x, z, type = "b", pch = 22, col = "blue", lty = 2)
> axis(2, at = x, labels = x, col.axis = "red", las =2)
> axis(4, at = z, labels = round(z, digits = 2), col.axis = "blue", las = 2, cex.axis = 0.7, tck = -.01)
> mtext("y = 1 / x", side = 4, line = 3, cex.lab = 1, las = 2, col = "blue")
> title("An Example of Creative Axes", xlab = "X values", ylab = "Y = X")
> par(opar)

3.4.3 參考線

abline(h = tvalues, v = xvalues)
abline(h = c(1, 5, 7))
abline(v = seq(1, 10, 2), lyt = 2, col = "blue")

3.4.4 圖例

legend(location, title, legend, ...)
> dose <- c(20, 30, 40, 45, 60)
> drugA <- c(16, 20, 27, 40, 60)
> drugB <- c(15, 18, 25, 31, 40)
> opar <- par(no.readonly = TRUE)
> par(lwd = 2, cex = 1.5, font.lab = 2)
> plot(dose, drugA, type = "b", pch = 15, lty = 1, col = "red", ylim = c(0, 60), main = "Drug A vs. Drug B", xlab = "Drug Dosage", ylab = "Drug Response")
> lines(dose, drugB, type = "b", pch = 17, lty = 2, col = "blue")
> abline(h = c(30), lwd = 1.5, lty = 2, col = "gray")
> minor.tick(nx = 3, ny = 3, tick.ratio = 0.5)
> legend("topleft", inset = 0.5, title = "Drug Type", c("A","B"),lty = c(1, 2), pch = c(15, 17), col = c("red", "blue"))
> par(opar)

3.4.5 文本標注

text(location, "text to place", pos, ...)
mtext("text to place", side, line = n, ...)

3.4.6 數學標注 P61

plotmath()

3.5 圖形的組合

attach(mtcars)
opar <- par(no.readonly = TRUE)
par(mfrow = c(2, 2))
plot(...)
plot(...)
hist(...)
boxplot(...)
par(opar)
detach(mtcars)
attach(mycars)
layout(matrix(c(1, 1, 2, 3), 2, 2, byrow = TRUE), widths = c(3,1), heights = c(1, 2))
hist(...)
hist(...)
hist(...)
detach(mtcars)

圖形布局的精細控制

opar <- par(no.readonly = TRUE)
par(fig = c(0, 0.8, 0, 0.8))
plot(...)
par(fig = c(0, 0.8, 0.55, 1), new = TRUE)
boxplot(...)
par(fig = c(0.65, 1, 0, 0.8, new = TRUE))
boxplot(...)
par(opar)

第4章 基本數據管理

4.2 創建新變量

可能需要創建新變量或者對現有的變量進行變換。變量名 <- 表達式

mydata <- data.frame(x1 = c(2, 2, 6, 4), x2 = c(3, 4, 2, 8))
#方式一
mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2) / 2
#方式二
attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2) / 2
detach(mydata)
#方式三
mydata <- transform(mydata, sumx = x1 +x2, meanx = (x1 + x2) / 2)

4.3 變量的重編碼

leadership <- within(leadership, {
                                      agecat <- NA
                                      agecat[age >75] <- "Elder"
                                      agecat[age >= 75 & age <= 75] <- "Middle Aged"
                                      agecat[age < 55] <- "Young"})

其他實用的變量重編碼函數:car包中的recode()、doBy包中的recodevar()、R自帶的cut()。

4.4 變量的重命名

  1. fix(leadership)調用交互式編輯器。
  2. names()函數重命名變量:
names(leadership)[2] <- "testDate"
names(leadership)[6:10] <- c("item1", "item2", "item3", "item4", "item5") 
  1. plyr包中的rename()函數可用于修改變量名:
library(plyr)
leadership <- rename(leadership, c(manager = "managerID", date = "testDate"))

4.5 缺失值

缺失值以符號NA(Not Available,不可用)表示。
函數is.na()允許檢測缺失值是否存在。

y <- c(1, 2, 3, NA)
is.na(y)
返回c(FALSE, FALSE, FALSE, TRUE)
is.na(leadership[,6:10])
#將數據框限定到第6到第10列

識別無限的或者不可能出現的數值用is.infinite()或is.nan()。

4.5.1 重編碼某些值為缺失值

要確保所有的缺失數據已在分析之前被妥善地編碼為缺失值。

leadership$age[leadership$age == 99] <- NA

4.5.2 在分析中排除缺失值

  • 多數的數值函數擁有一個na.rm = TRUE選項,可以在計算之前移除缺失值并使用剩余值進行計算:
x <- c(1, 2, NA, 3)
y <- sum(x, na.rm = TRUE)
  • na.omit()可以刪除所有含有缺失數據的行:
newdata <- na.omit(leadership)

4.6 日期值

as.Date(x, "input_format"),默認輸入格式為yyyy-mm-dd。

dates <- as.Date(strDates, "%m/%d/%Y")
myformat <- "%m/%d/%y"
leadership$date <- as.Date(leadership$date, myformat)

Sys.Date()返回當天的日期。
date()返回當前的日期和時間。
可以使用函數format(x, format = "output_format")來輸出指定格式的日期值,并且可以提取日期值中的某些部分:

today <- Sys.Date()
format(today, format = "%B %d %Y")
format(today, format = "%A")

R的內部存儲日期是使用自1970年1月1日以來的天數表示的,更早的日期表示為負數。可以在日期值上執行算術運算:

> startdate <- as.Date("2004-02-13")
> enddate <- as.Date("2011-01-22")
> days <- enddate - startdate
> days
Time difference of 2535 days

> today <- Sys.Date()
> dob <- as.Date("1996-09-05")
> difftime(today, dob, units = "days")
Time difference of 7640 days

4.6.1 將日期轉換為字符型變量

strDates <- as.character(dates)

4.7 類型轉換 P78

> a <- c(1, 2, 3)
> a
[1] 1 2 3
> is.numeric(a)
[1] TRUE
> is.vector(a)
[1] TRUE
> a <- as.character(a)
> a
[1] "1" "2" "3"
> is.numeric(a)
[1] FALSE
> is.vector(a)
[1] TRUE
> is.character(a)
[1] TRUE

4.8 數據排序

order()函數對一個數據框進行排序,默認升序,在排序變量前邊加一個減號可得降序。

newdata <- leadership[order(leadership$age),]
#創建新的數據集,其中各行依照經理人的年齡升序排序
attach(leadership)
newdata <- leadership[order(gender, age), ]
detach(leadreship)
#各行依照女性到男性、同樣性別中按年齡升序排序
attach(leadership)
newdata <- leadership[order(gender, -age), ]
detach(leadreship)
#各行依照女性到男性、同樣性別中按年齡降序排序

4.9 數據集的合并

4.9.1 向數據框添加列

橫向合并兩個數據框(數據集),使用merge()函數。

total <- merge(dataframeA, dataframeB, by = "ID")

total <- merge(dataframeA, dataframeB, by = c("ID", "Country"))

#如果直接橫向合并兩個矩陣或者數據框并且不指定一個公共索引用cbind()
total <- cbind(A, B)
#每個對象必須擁有相同的行數,以同順序排序

4.9.3 向數據框添加行

縱向合并兩個數據框(數據集)使用rbind()函數:

total <- rbind(dataframeA, dataframeB)

兩個數據框必須擁有相同的變量,不過順序不必一定相同。

4.10 數據集取子集

4.10.1 選入(保留)變量

newdata <- leadership[, c(6:10)]

myvars <- c("q1", "q2", "q3", "q4", "q5")
newdata <- leadership[myvars]

myvars <- paste("q", 1:5, sep=" ")
newdata <- leadership[myvars]

4.10.2 剔除(丟棄)變量

myvars <- names(leadership) %in% c("q3", "q4")
newdata <- leadership[!myvars]

#已知q3和q4是第8個和第9個變量的情況下,可以使用語句:
newdata <- leadership[c(-8, -9)]

#設置q3和q4兩列為未定義(NULL)亦是一種刪除方式,NULL和NA是不同的
leadership$q3 <- leadership$q4 <-NULL

4.10.3 選入觀測

newdata <- leadership[1:3, ]

newdata <- leadership[leadership$gender == "M" & leadership$age >30,]

leadership$data <- as.Date(LEADERSHIP$DATE, "%m/%d/%y")
startdate <- as.Date("2009-01-01")
enddate <- as.Date("2009-10-31")
newdata <- leadership[which(leadership$date) >= startdate & leadership$date <= enddate), ]

4.10.4 subset()函數

選擇變量和觀測最簡單的方法。

newdata <- subset(leadership, age >= 35 | age < 24, select = c(q1, q2, q3, q4))
newdate <- subset(leadership, gender == "M" & age > 25, select = gender:q4)

4.10.5 隨機抽樣

sample()函數中的第一個參數是一個由要從中抽樣的元素組成的向量,第二個參數是要抽取的元素數量, 第三個參數表示無放回抽樣。

mysample() <- leadership[sample(1:nrow(leadership), 3, replace = FALSE), ]

4.11 使用SQL語句操作數據框

library(sqldf)
newdf <- sqldf("select * from mtcars where carb = 1 order by mpg", row.names = TRUE)

sqldf("select avg(mpg) as avg_mpg, avg(disp) as avg_disp, gear from mtcars where cyl in (4, 6) group by gear")

第5章 高級數據管理

5.2 數值和字符處理函數

5.2.1 數學函數 P86

5.2.2 統計函數 P88

5.2.3 概率函數 P90

 R中概率函數形如[dpqr]distribution_abbreviation()
#位于z = 1.96左側的標準正態曲線下方面積是多少?
> pnorm(1.96)
[1] 0.9750021
#均值為500,標準差為100的正態分布的0.9分位點值為多少?
> qnorm(.9, mean = 500, sd = 100)
[1] 628.1552
#生成50個均值為50,標準差為10的正態隨機數
> rnorm(50, mean = 50, sd = 10)
  1. 設定隨機數種子
    set.seed()顯示指定種子。runif()生成0到1區間上服從均勻分布的偽隨機數。
> runif(5)
[1] 0.6509069 0.9809366 0.2417076 0.4011322 0.1121973
> runif(5)
[1] 0.95703624 0.86061820 0.09811243 0.74588111 0.75219763
> set.seed(1234)
> runif(5)
[1] 0.1137034 0.6222994 0.6092747 0.6233794 0.8609154
> runif(5)
[1] 0.640310605 0.009495756 0.232550506 0.666083758 0.514251141
> set.seed(1234)
> runif(5)
[1] 0.1137034 0.6222994 0.6092747 0.6233794 0.8609154
  1. 生成多元正態數據
    MASS包中的mvrnorm()函數:mvrnorm(n, mean, sigma)
    其中n是想要的樣本大小,mean為均值向量,而sigma是方差-協方差矩陣(或相關矩陣)
> library(MASS)
> options(digits = 3)
> set.seed(1234)

> mean <- c(230.7, 146.7, 3.6)
> sigma <- matrix(c(15360.8, 6721.2, -47.1, 6721.2, 4700.9, -16.5, -47.1, -16.5, 0.3), nrow = 3, ncol = 3)

> mydata <- mvrnorm(500, mean, sigma)
> mydata <- as.data.frame(mydata)
> names(mydata) <- c("y", "x1", "x2")

> dim(mydata)
[1] 500   3
> head(mydata, n = 10)
       y    x1   x2
1   98.8  41.3 3.43
2  244.5 205.2 3.80
3  375.7 186.7 2.51
4  -59.2  11.2 4.71
5  313.0 111.0 3.45
6  288.8 185.1 2.72
7  134.8 165.0 4.39
8  171.7  97.4 3.64
9  167.2 101.0 3.50
10 121.1  94.5 4.10

5.2.4 字符處理函數 P93

5.2.5 其他實用函數 P94

5.2.6 將函數應用于矩陣和數據框

apply()函數:apply(x, MARGIN, FUN, ...)
x為數據對象, MARGIN是維度的下標, FUN是制定的函數,...包括了任何想傳遞給FUN的參數。在矩陣或數據框中,MARGIN = 1表示行, MARGIN = 2表示列。

5.4 控制流

5.4.1 重復和循環

  1. for結構
#for (var in seq) statement
for (i in 1:10) print("Hello")
  1. while結構
#while (cond) statement
i <- 10
while (i > 0) {print("Hello"); i <- i - 1}

5.4.2 條件執行

  1. if-else結構
#if (cond) statement
#if (cond) statement1 else statement2
if (is.character(grade)) grade <- as.factor(grade)
if (!is.factor(grade)) grade <- as.factor(grade) else print("Grade already is a factor")
  1. ifelse結構
#ifelse(cond, statement1, statement2)
ifelse(score > 0.5, print("Passed"), print("Failed"))
outcome <- ifelse(score > 0.5, "Passed", "Failed")
  1. switch結構
#switch(expr, ...)
> feelings <- c("sad", "afraid")
> for (i in feelings)
+     print(
+         switch(i, happy = "I am glad you are happy",
+                   afraid = "There is nothing to fear",
+                   sad = "Cheer up",
+                   angry = "Calm down now"
+         )
+     )
[1] "Cheer up"
[1] "There is nothing to fear"

5.5 用戶自編函數

myfunction <- function (arg1, arg2, ...) {
    statements
    return(object)
}

5.6 整合與重構

5.6.1 轉置

使用函數t()對一個矩陣或數據框進行轉置。數據框行名將變成變量(列)名。

> cars <- mtcars[1:5, 1:4]
> cars
                   mpg cyl disp  hp
Mazda RX4         21.0   6  160 110
Mazda RX4 Wag     21.0   6  160 110
Datsun 710        22.8   4  108  93
Hornet 4 Drive    21.4   6  258 110
Hornet Sportabout 18.7   8  360 175
> t(cars)
     Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
mpg         21            21       22.8           21.4              18.7
cyl          6             6        4.0            6.0               8.0
disp       160           160      108.0          258.0             360.0
hp         110           110       93.0          110.0             175.0

5.6.2 整合數據

使用一個或多個by變量和一個預先定義好的函數來折疊(collapse)數據。
調用格式為:aggregate(x, by, FUN)
其中x是待折疊的數據對象,by是一個變量名組成的列表,這些變量將被去掉以形成新的觀測,而FUN則是用來計算描述性統計量的標量函數,他將被用來計算新觀測中的值。

> options(digits = 3)
> attach(mtcars)
> aggdata <- aggregate(mtcars, by = list(cyl, gear), FUN = mean, na.rm = TRUE)
> aggdata
  Group.1 Group.2  mpg cyl disp  hp drat   wt qsec  vs   am gear carb
1       4       3 21.5   4  120  97 3.70 2.46 20.0 1.0 0.00    3 1.00
2       6       3 19.8   6  242 108 2.92 3.34 19.8 1.0 0.00    3 1.00
3       8       3 15.1   8  358 194 3.12 4.10 17.1 0.0 0.00    3 3.08
4       4       4 26.9   4  103  76 4.11 2.38 19.6 1.0 0.75    4 1.50
5       6       4 19.8   6  164 116 3.91 3.09 17.7 0.5 0.50    4 4.00
6       4       5 28.2   4  108 102 4.10 1.83 16.8 0.5 1.00    5 2.00
7       6       5 19.7   6  145 175 3.62 2.77 15.5 0.0 1.00    5 6.00
8       8       5 15.4   8  326 300 3.88 3.37 14.6 0.0 1.00    5 6.00

5.6.3 reshape2 包

reshape2包是一套重構和整合數據集的絕妙的萬能工具。使用前需安裝。

  1. 融合 P106
  2. 重鑄 P106

第6章 基本圖形

6.1 條形圖

barplot(height)
其中的height是一個向量或一個矩陣。使用參數horiz = TRUE生成水平條形圖。

6.1.1 簡單的條形圖

> library(grid)
> library(vcd)
> counts <- table(Arthritis$Improved)
> counts

  None   Some Marked 
    42     14     28 
> barplot(counts, main = "Simple Bar Plot", xlab = "Improvement", ylab = "Frequency")
> barplot(counts, main = "Horizontal Bar Plot", xlab = "Frequency", ylab = "Improvement", horiz = TRUE)

若要繪制的類別型變量是一個因子或有序型因子,可以用plot()快速創建垂直條形圖,無需使用table()將其表格化:

> plot(Arthritis$Improved, main = "Simple Bar Plot", xlab = "Improvement", ylab = "Frequency")
> plot(Arthritis$Improved, main = "Horizontal Bar Plot", xlab = "Frequency", ylab = "Improvement", horiz = TRUE)

6.1.2 堆砌條形圖和分組條形圖

如果height是矩陣,繪圖結果是堆砌條形圖或分組條形圖(beside = TRUE)。

> barplot(counts, main = "Stacked Bar Plot", xlab = "Treatment", ylab = "Frequency", col = c("red", "yellow", "green"), legend = rownames(counts))
> barplot(counts, main = "Grouped Bar Plot", xlab = "Treatment", ylab = "Frequency", col = c("red", "yellow", "green"), legend = rownames(counts), beside = TRUE)

6.1.3 均值條形圖

可以使用數據整合函數將結果傳遞給barplot()函數,來創建表示均值、中位數、標準差等的條形圖。

> states <- data.frame(state.region, state.x77)
> means <- aggregate(states$Illiteracy, by = list(state.region), FUN = mean)
> means
        Group.1        x
1     Northeast 1.000000
2         South 1.737500
3 North Central 0.700000
4          West 1.023077
> means <- means[order(means$x),]
> means
        Group.1        x
3 North Central 0.700000
1     Northeast 1.000000
4          West 1.023077
2         South 1.737500
> barplot(means$x, names.arg = means$Group.1)
> title("Mean Illiteracy Rate")

6.1.4 條形圖的微調

#增加y邊界的大小
> par(mar = c(5, 8, 4, 2))
#旋轉條形的標簽
> par(las = 2)
> counts <- table(Arthritis$Improved)
#縮小字體大小,修改標簽文本
> barplot(counts, main = "Treatment Outcome", horiz = TRUE, cex.names = 0.8, names.arg = c("No Improvement", "Some Improvement", "Marked Improvement"))

6.1.5 棘狀圖

對堆砌條形圖進行重縮放,每個條形的高度均為1,每一段的高度即表示比例。
由vcd包中的函數spine()繪制。

> par(mar = c(5, 8, 4, 2))
> par(las = 2)
> counts <- table(Arthritis$Improved)
> barplot(counts, main = "Treatment Outcome", horiz = TRUE, cex.names = 0.8, names.arg = c("No Improvement", "Some Improvement", "Marked Improvement"))

6.2 餅圖

更推薦使用條形圖或點圖,相對于面積人們對長度的判斷更精確。
pie(x, labels)
其中x是一個非負數值向量,表示每個扇形的面積,labels則是表示各扇形標簽的字符型向量。

> par(mfrow = c(2, 2)) #四幅圖形組合成一幅
> slices <- c(10, 12, 4, 16, 8)
> lbls <- c("US", "UK", "Australia", "Germany", "France")
> pie(slices, labels = lbls, main = "Simple Pie Chart")
 
> pct <- round(slices / sum(slices) * 100)
> lbls2 <- paste(lbls, " ", pct, "%", sep = "")
> pie(slices, labels = lbls2, col = rainbow(length(lbls2)), main = "Pie Chart with Percentages")

> library(plotrix)
> pie3D(slices, labels = lbls, explode = 0.1, main = "3D Pie Chart")

> mytable <- table(state.region) #從表格創建餅圖
> lbls3 <- paste(names(mytable), "\n", mytable, sep = "")
> pie(mytable, labels = lbls3, main = "Pie Chart from a Table\n (with sample sizes)")

餅圖難以比較值,扇形圖提供了一種同時展示相對數量和相互差異的方法。
plotrix中的包fan.plot()實現。

> library(plotrix)
> slices <- c(10, 12, 4, 16, 8)
> lbls <- c("US", "UK", "Australia", "Germany", "France")
> fan.plot(slices, labels = lbls, main = "Fan Plot")

6.3 直方圖

直方圖描述連續型變量的分布,通過在x軸上將值域分割為一定數量的組,在y軸上顯示相應值的頻數。
hist(x)
其中x是一個由數據值組成的數值向量。參數freq = FALSE表示根據概率密度而不是頻數繪制圖形。參數breaks用于控制組的數量。

> par(mfrow = c(2, 2))
> hist(mtcars$mpg)
> mtcars$mpg
#指定組數和顏色
> hist(mtcars$mpg, breaks = 12, col = "red", xlab = "Miles Per Gallon", main = "Colored histogram with 12 bins")
#添加軸須圖
> hist(mtcars$mpg, freq = FALSE, breaks = 12, col = "red", xlab = "Miles Per Gallon", main = "Histogram, rug plot, density curve")
> rug(jitter(mtcars$mpg))
> lines(density(mtcars$mpg), col = "blue", lwd = 2)
#添加正態密度曲線和外框
> x <- mtcars$mpg
> h <- hist(x, breaks = 12, col = "red", xlab = "Miles Per Gallon", main = "Histogram with normal curve and box")
> xfit <- seq(min(x), max(x), length = 40)
> yfit <- dnorm(xfit, mean = mean(x), sd = sd(x))
> yfit <- yfit * diff(h$mids[1:2] * length(x))
> lines(xfit, yfit, col = "blue", lwd  = 2)
> box()

如果數據有許多結(相同的值),rug(jitter(mtcars$mpag, amount = 0.01)),向每個數據點添加一個小的隨機值(一個±amount之間的均勻分布隨機數),以避免重疊的點產生影響。

6.4 核密度圖

核密度估計是用于估計隨機變量概率密度函數的一種非參數方法。
plot(density(x))
其中的x是一個數值型向量。plot()函數會創建一幅新的圖形,所以要向一幅已經存在的圖形上疊加一條密度曲線,可以使用lines()函數(見上文)。

> par(mfrow = c(2, 1))
> d <- density(mtcars$mpg)
> plot(d)
> plot(d, main = "Kernel Dendity of Miles Per Gallon")
> polygon(d, col = "red", border = "blue") #polygon()函數根據定點的x和y坐標繪制多邊形
> rug(mtcars$mpg, col = "brown")

核密度圖可用于比較組間差異,使用sm包中的sm.density.compare()函數可向圖形疊加兩組或更多的核密度圖:sm.density.compare(x, factor)
其中的x是一個數值型向量,factor是一個分組變量。

> library(sm)
> attach(mtcars)
#創建分組因子
> cyl.f <- factor(cyl, levels = c(4, 6, 8), labels = c("4 cylinder", "6 cylinder", "8 cylinder"))
#繪制密度圖
> sm.density.compare(mpg, cyl, xlab = "Miles Per Gallon")
> title(main = "MPG Distribution by Car Cylinders")
#通過鼠標單擊添加圖例
> colfill <- c(2:(1 + length(levels(cyl.f))))
> legend(locator(1), levels(cyl.f), fill = colfill)
> detach(mtcars)

6.5 箱線圖

箱線圖(盒須圖)是一項用來可視化分布和組間差異的絕佳圖形手段(更常用)。
通過繪制連續型變量的五數總括,即最小值、下四分位數(第25百分位數)、中位數(第50百分位數)、上四分位數(第75百分位數)以及最大值,描述了連續型變量的分布。箱線圖能夠顯示出可能為離群點(范圍±1.5*IQR以外的值,IQR表示四分位距,即上四分位數與下四分位數的差值)的觀測。

> boxplot(mtcars$mpg, main = "Box plot", ylab = "Miles per Gallon")
#輸出用于構建圖形的統計量
> boxplot.stats(mtcars$mpg)

6.5.1 使用并列箱線圖進行跨組比較

箱線圖可以展示單個變量或分組變量。使用格式為:
boxplot(formula, data = dataframe)
其中的formula是一個公式,dataframe代表提供數據的數據框或列表。
一個實例公式為y ~ A,這將為類別型變量A的每個值并列地生成數值型變量y的箱線圖。公式y ~ A*B則將為類別型變量A和B所有水平的兩兩組合生成數值型變量y的箱線圖。
添加參數varwidth = TRUE將使箱線圖的寬度與其樣本大小的平方根成正比。參數horizontal = TRUE可以反轉坐標軸的方向。

> attach(mtcars)
> boxplot(mpg ~ cyl, data = mtcars, main = "Car Mileage Data", xlab = "Number of Cylinders", ylab = "Miles Per Gallon")
#添加notch = TRUE得到含凹槽的箱線圖。
> boxplot(mpg ~ cyl, data = mtcars, notch = TRUE, col = "red", main = "Car Mileage Data", xlab = "Number of Cylinders", ylab = "Miles Per Gallon")
> detach(mtcars)

兩個交叉因子的箱線圖:

#創建汽缸數量的因子
> mtcars$cyl.f <- factor(mtcars$cyl, levels = c(4, 6, 8), labels = c("4", "6", "8"))
#創建變速箱類型的因子
> mtcars$am.f <- factor(mtcars$am, levels = c(0, 1), labels = c("anto", "standard"))
> boxplot(mpg ~ am.f * cyl.f, data = mtcars, varwidth = TRUE, col = c("gold", "darkgreen"), main = "MPG Distribution by Auto Type", xlab = "Auto Type", ylab = "Miles Per Gallon")

6.5.2 小提琴圖

箱線圖與核密度圖的結合,使用vioplot包中的vioplot()函數繪制。
vioplot(x1, x2, ... , names = , col = )
其中x1, x2, ...表示要繪制的一個或多個數值向量(將為每個向量繪制一幅小提琴圖),參數names是小提琴圖中標簽的字符向量,而col是一個為每幅小提琴圖指定顏色的向量。

> library(vioplot)
> x1 <- mtcars$mpg[mtcars$cyl == 4]
> x2 <- mtcars$mpg[mtcars$cyl == 6]
> x3 <- mtcars$mpg[mtcars$cyl == 8]
> vioplot(x1, x2, x3, names = c("4 cyl", "6 cyl", "8 cyl"), col = "gold")
> title("Violin Plots of Miles Per Gallon", ylab = "Miles Per Gallon", xlab = "Number of Cylinders")

6.6 點圖

點圖繪制變量中的所有值,提供了一種在水平刻度上繪制大量有標簽值的方法。
dotchart(x, labels = )
其中的x是一個數值向量,而labels則是由每個點的標簽組成的向量。可以通過添加參數groups來選定一個因子,用以指定x中元素的分組方式。如果這樣做,gcolor可以控制不同組標簽的顏色,cex可以控制標簽的大小。

> dotchart(mtcars$mpg, labels = row.names(mtcars), cex = .7, main = "Gas Mileage for Car Models", xlab = "Miles Per Gallon")
#分組、排序、著色后的點圖
> x <- mtcars[order(mtcars$mpg), ]
> x$cyl <- factor(x$cyl)
> x$color[x$cyl == 4] <- "red"
> x$color[x$cyl == 6] <- "blue"
> x$color[x$cyl == 8] <- "darkgreen"
> dotchart(x$mpg, labels = row.names(x), cex = .7, groups = x$cyl)
> dotchart(x$mpg, labels = row.names(x), cex = .7, groups = x$cyl, gcolor = "black", color = x$color, pch = 19, main = "Gas Mileage for Car Models\n grouped by cylinder", xlab = "Miles Per Gallon")

點圖有許多變種,Hmisc包提供了一個帶有許多附加功能的點圖函數dotchart2。

第7章 基本統計分析

7.1 描述性統計分析

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,743評論 2 370

推薦閱讀更多精彩內容