前言:
網(wǎng)易云課堂R語言數(shù)據(jù)科學(xué)入門聽課筆記措译,主要講述R和RStudio的安裝使用,R基本語法以及R包的使用表悬。
封面致敬R大神Hadley Wickham
Hadley Wickham is a statistician from New Zealand who is currently Chief Scientist at RStudio[1][2] and an adjunct Professor of statistics at the University of Auckland[3], Stanford University[4], and Rice University.[5] He is best known for his development of open-source statistical analysis software packages for R (programming language) that implement logics of data visualisation and data transformation. Wickham's packages and writing are known for advocating a tidy data approach to data import, analysis and modelling methods.
R初試:
#繪制直方圖
> hist(rnorm(10000),col='skyblue')
Rstudio基本操作:
1. 注釋:
- “#”后面的內(nèi)容表示注釋
- 如果有多行注釋則在每一行注釋前加上“#”
- 在注釋行結(jié)尾加上4個(gè)“#”夕凝,則這一行會(huì)被當(dāng)做一個(gè)節(jié)標(biāo)題
2. 變量賦值:
- “<-” 表示變量賦值
- “=” 表示參數(shù)賦值
3. 數(shù)據(jù)調(diào)用:
- head(diamonds) R語法
- diamonds.head() Python語法
打開文件亂碼情況茎杂,解決方案:
file > reopen with encoding > UTF-8 / set as default
4. 查看幫助文檔:
- help(ggplot2) #對(duì)包使用help,查看包的信息
- help("CO2") #對(duì)函數(shù)使用help摔竿,查看函數(shù)使用方法
- help(ggplot) #對(duì)內(nèi)置數(shù)據(jù)集使用幫助文檔真慢,查看數(shù)據(jù)集的信息
- example(ggplot) # 查看ggplot函數(shù)用法示例
5. 使用R的內(nèi)置數(shù)據(jù)集
- data() #用于查看R語言的內(nèi)置數(shù)據(jù)集
- head(women) #直接鍵入數(shù)據(jù)集名稱查看數(shù)據(jù)集
- install.packages("gcookbook") # 安裝gcookbook
- data(package =.packages(all.available =TRUE))#查看已安裝的所有包中的數(shù)據(jù)
- library(gcookbook) #使用其它包中的數(shù)據(jù),需要先載入相應(yīng)的R包
- head(worldpop) #World population estimates from 10,000 B.C. to 2,000A.D.
什么是R包榕吼?
R package, 中文是 R 包. R 包是一種包裝 R 代碼, 數(shù)據(jù)和文檔的方式. R 包是 R 語言強(qiáng)大的擴(kuò)展性和靈活性的基礎(chǔ), R 發(fā)行版本身就包括三十多個(gè)高質(zhì)量的 R 包. 一個(gè)R包中包含了一組可以完成特定任務(wù)的程序代碼饿序;通常這種特定任務(wù)是會(huì)重復(fù)執(zhí)行的,并且重復(fù)執(zhí)行時(shí)羹蚣,任務(wù)內(nèi)容是相似或者相同的原探。 在使用R分析數(shù)據(jù)時(shí),其中一部分代碼是我們自己寫的,還有很大一部分代碼是通過調(diào)用其他人寫好的R包實(shí)現(xiàn)的咽弦。如果沒有這些第三方R包徒蟆,我們的生活將會(huì)在加班和過度勞累中失去意義。
1. 通過菜單點(diǎn)擊安裝R包
- 1.1在R原生界面中安裝
- 1.2在Rstudio中安裝
2. 通過命令安裝R包
install.packages('ggplot2') # 一次一個(gè)
install.packages(c('reshape2','ggthemes','plyr')) # 一次多個(gè)
3. 切換R包鏡像型型,以便更快下載R包
- 3.1 通過菜單操作
- 3.2 通過程序命令設(shè)定
# R包下載鏡像列表
https://cran.r-project.org/mirrors.html
# 全局生效
options(repos = 'https://mirrors.ustc.edu.cn/CRAN/')
# 只在下載此R包時(shí)生效
install.packages('ggplot2',repos ='https://mirrors.ustc.edu.cn/CRA
4. 常用的R包
- 數(shù)據(jù)加載
- readr,readxl,haven: 存取本地文件
- RODBC,RMySQL,RPOstgresSQL, RSQLite :從數(shù)據(jù)庫中存取數(shù)據(jù)
- 數(shù)據(jù)處理
- dplyr - 必備數(shù)據(jù)處理工具段审,對(duì)數(shù)據(jù)集做summarize, join等處理。
- tidyr - 將數(shù)據(jù)集轉(zhuǎn)化成格式更工整的數(shù)據(jù)集闹蒜,數(shù)據(jù)清洗寺枉。
- stringr - 對(duì)字符串類型的數(shù)據(jù)進(jìn)行正則表達(dá)式處理的工具。
- lubridate - 處理日期和時(shí)間類型數(shù)據(jù)的工具
- 數(shù)據(jù)可視化
- ggplot2: R中最著名的可視化工具包
5. R知識(shí)體系概覽
數(shù)據(jù)文件導(dǎo)入和導(dǎo)出點(diǎn)我觀看視頻
部分windows系統(tǒng)用戶绷落,無法識(shí)別中文型凳,需要改成英文字符,像我“R語言數(shù)據(jù)科學(xué)”就改成了“yuyanshujukexue”
1. 導(dǎo)入csv文件
> bankloan<-read.csv('c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/bankloan.csv',header = TRUE)
> View(bankloan)
但是用R自身無法讀取成功嘱函,需要用到readr包
> install.packages("readr")
> library(readr)
> bankloan <- read_csv('c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/bankloan.csv')
Parsed with column specification:
cols(
ID = col_character(),
年齡 = col_integer(),
教育 = col_character(),
工齡 = col_integer(),
地址 = col_integer(),
收入 = col_integer(),
負(fù)債率 = col_double(),
信用卡負(fù)債 = col_double(),
其他負(fù)債 = col_double(),
違約 = col_character()
)
2. 導(dǎo)入U(xiǎn)TF-8編碼的csv文件
# 可能會(huì)出現(xiàn)問題
> bankloan<-read.csv('c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/bankloan_UTF-8.csv',
header = TRUE,
encoding = 'UTF-8')
# 推薦使用這種方式
> bankloan_utf_8<-readr::read_csv('c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/bankloan_UTF-8.csv')
> View(bankloan_utf_8)
3. 導(dǎo)入Excel格式的數(shù)據(jù)文件
library(readxl)
> bankloan_excel<-read_excel('c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/bankloan.xlsx',
sheet = 'bankloan')
> View(bankloan_excel)
4. 將數(shù)據(jù)存儲(chǔ)到本地,格式為csv甘畅,編碼方式為GBK
> library(ggplot2)
> head(diamonds)
# A tibble: 6 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
4 0.290 Premium I VS2 62.4 58 334 4.2 4.23 2.63
5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
> write.csv(diamonds,
file = 'c:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/diamonds.csv',
fileEncoding = 'GBK',
row.names = FALSE)
5. 使用相對(duì)路徑
> setwd('C:/Users/Administrator/Documents/01-Ryuyanshujukexue/data/')
> bankloan_relative<-read_csv('./bankloan.csv')
> head(bankloan_relative)
# A tibble: 6 x 10
ID 年齡 教育 工齡 地址 收入 負(fù)債率 信用卡負(fù)債 其他負(fù)債 違約
<chr> <int> <chr> <int> <int> <int> <dbl> <dbl> <dbl> <chr>
1 ID_001 41 大專 17 12 176 9.3 11.4 5.01 是
2 ID_002 27 未完成高中 10 6 31 17.3 1.36 4.00 否
3 ID_003 40 未完成高中 15 14 55 5.5 0.856 2.17 否
4 ID_004 41 未完成高中 15 14 120 2.9 2.66 0.821 否
5 ID_005 24 高中 2 0 28 17.3 1.79 3.06 是
6 ID_006 41 高中 5 5 25 10.2 0.393 2.16 否
6. 導(dǎo)入其它統(tǒng)計(jì)軟件的數(shù)據(jù)
> library(haven)
> bankloan_spss <- read_spss('./bankloan.sav')
> head(bankloan_spss)
# A tibble: 6 x 9
年齡 教育 工齡 居住年限 收入 負(fù)債率 信用卡負(fù)債 其他負(fù)債 違約
<dbl> <dbl+lbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl+lbl>
1 41 3 17 12 176 9.3 11.4 5.01 1
2 27 1 10 6 31 17.3 1.36 4.00 0
3 40 1 15 14 55 5.5 0.856 2.17 0
4 41 1 15 14 120 2.9 2.66 0.821 0
5 24 2 2 0 28 17.3 1.79 3.06 1
6 41 2 5 5 25 10.2 0.393 2.16 0
7. 將R內(nèi)置數(shù)據(jù)集存儲(chǔ)為spss軟件數(shù)據(jù)格式
> require(ggplot2)
> haven::write_sav(diamonds,path = './diamonds.sav')
結(jié)果展示
- 用Rmarkdown 輸出分析報(bào)告
內(nèi)容為Rmarkdown的用法概述,感興趣的可以深入學(xué)習(xí)Rmarkdown用法往弓,和簡書有相似之處疏唾。 -
在 Jupyter notebook 中運(yùn)行R代碼
Jupyter Notebook是一個(gè)Web應(yīng)用程序,允許您創(chuàng)建和共享包含實(shí)時(shí)代碼函似,方程槐脏,可視化和說明文本的文檔。 用途包括:數(shù)據(jù)清理和轉(zhuǎn)換撇寞,數(shù)值模擬顿天,統(tǒng)計(jì)建模,機(jī)器學(xué)習(xí)等等蔑担。
……