《R语言实战》第2章
生活随笔
收集整理的這篇文章主要介紹了
《R语言实战》第2章
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
# 2.2.1 向量
# 數(shù)值型向量
a <- c(1, 2, 3, 4, -2, 6)
# 字符型向量
b <- c("one", "two", "three")
# 邏輯型向量
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)# 訪問向量
# 訪問第幾個元素
a[3]
a[c(1, 3, 5)]
# 等價于a <- c(2, 3, 4, 5, 6)。
a[2:6] # 2.2.2 矩陣
# 創(chuàng)建矩陣
y <- matrix(1:20, nrow = 5, ncol = 4)
# 定義矩陣向量數(shù)據(jù)
cells <- c(1, 26, 24, 58)
# 定義行名稱
rnames <- c("R1", "R2")
# 定義列名稱
cnames <- c("C1", "C2")
# 按行填充
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = TRUE,dimnames = list(rnames, cnames))
# 按列填充
mymatrix <- matrix(cells, nrow = 2, ncol = 2, byrow = FALSE,dimnames = list(rnames, cnames))
# 矩陣下標(biāo)的使用
x <- matrix(1:10, nrow = 2)
# 顯示第二行
x[2, ]
# 顯示第二列
x[, 2]
# 顯示第一行第四個元素
x[1, 4]
# 顯示第一行第四、五列的元素
x[1, c(4, 5)]# 2.2.3 數(shù)組
# 創(chuàng)建一個數(shù)組
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))# 2.2.4 數(shù)據(jù)框
# 創(chuàng)建一個數(shù)據(jù)框
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)
# 選取數(shù)據(jù)框中的元素
# 查看一二列的元素
patientdata[1:2]
# 通過名字查詢
patientdata[c("diabetes", "status")]
# 通過$查詢
patientdata$age# 使用三個函數(shù)簡化代碼:attach()、detach()和with()
# 之前的寫法
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)
# with:大括號{}之間的語句都針對數(shù)據(jù)框mtcars執(zhí)行,這樣就無須擔(dān)心名稱沖突 了。如果僅有一條語句(例如summary(mpg)),那么大括號{}可以省略
with(mtcars, {summary(mpg, disp, wt)plot(mpg, disp)plot(mpg, wt)
})
# <<-:建在with()結(jié)構(gòu)以外存在的對象,使用特殊賦值符<<-替代標(biāo)準(zhǔn)賦值符(<-) 即可,它可將對象保存到with()之外的全局環(huán)境中
with(mtcars, {nokeepstats <- summary(mpg)keepstats <<- summary(mpg)
})
# nokeepstats: Error: object 'nokeepstats' not found
keepstats
# 實例標(biāo)識符:實例標(biāo)識 符(case identifier)可通過數(shù)據(jù)框操作函數(shù)中的rowname選項指定
patientdata <- data.frame(patientID, age, diabetes, status, row.names = patientID)# 2.2.5 因子
diabetes <- factor(diabetes)
# 要表示有序型變量,需要為函數(shù)factor()指定參數(shù)ordered=TRUE
status <- factor(status, ordered = TRUE)
# 可以通過指定levels選項來覆蓋默認(rèn)排序
status <- factor(status, ordered = TRUE,levels = c('Poor', 'Improved', 'Excellent'))# 2.2.6 列表
g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow = 5)
k <- c("one", "two", "three")
# 創(chuàng)建列表:列表允許整合若干(可能無關(guān)的)對象到單個對象名下。例如,某個列表中可能是若干向量、矩陣、數(shù)據(jù)框,甚至其他列表的組合。
mylist <- list(title = g, age = h, j, k)
mylist
# 查看age向量
mylist[[2]]
mylist[['age']]# 2.3.1 使用鍵盤輸入數(shù)據(jù)
mydata <- data.frame(age = numeric(0),gender = character(0),weight = numeric(0))
mydata <- edit(mydata) # macos上要下載XQuartz才能使用
mydata# 2.3.2 導(dǎo)入數(shù)據(jù)
grades <- read.table("studentgrades.csv", header = TRUE, sep = ",",row.names = "STUDENTID")
#設(shè)置選項 stringsAsFactors=FALSE,這將停止對所有字符型變量的此種轉(zhuǎn)換。另一種方法是使用選項colClasses為每一列指定一個類,例如logical(邏輯型)、numeric(數(shù)值型)、character (字符型)、factor(因子)# 2.3.3 導(dǎo)入Excel數(shù)據(jù)
install.packages("readxl")
library(readxl)
# 1、path:文件存儲路徑及文件名(含擴(kuò)展名),如Mac環(huán)境中輸入為:/Users/aiqingjiel/Desktop/data1.xlsx
# 2、col_name=TURE:第一行為列字段名,如col_name=FALSE,R以X01,X02,……,Xn來代替列字段名;
# 3、col_type=NULL:導(dǎo)入的電子表格或向量中可能包含空白,數(shù)值,日期或text文本;
# 4、na=“”,默認(rèn)講點子表哥中空白單元格轉(zhuǎn)換為缺失的值;
# 5、skip=0:在讀取數(shù)據(jù)前,每行數(shù)據(jù)不回有遺漏
excel <- read_excel('/Users/moxingjian/Work/bingo/蘭州地鐵項目/機(jī)電模塊問題.xlsx',sheet = 1, col_names = TRUE, col_types = NULL, na = '', skip = 0)summary(excel)
example("read_excel")# 值標(biāo)簽
patientdata$gender <- factor(patientdata$gender,levels = c(1, 2),labels = c("male", "female"))# 2.5 處理數(shù)據(jù)對象的實用函數(shù)
# 顯示對象中元素/成分的數(shù)量
length(patientdata)
# 顯示某個對象的維度
dim(patientdata)
# 顯示某個對象的結(jié)構(gòu)
str(patientdata)
# 顯示某個對象的類或類型
class(patientdata)
# 顯示某個對象的模式
mode(patientdata)
# 顯示某對象中各成分的名稱
names(patientdata)
# 顯示當(dāng)前的對象列表
ls(patientdata)
# 編輯對象并另存為newobject
newobject <- edit(patientdata)
# 編輯對象并另存為newobject
fix(patientdata)
newobject
總結(jié)
以上是生活随笔為你收集整理的《R语言实战》第2章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《R语言实战》第1章
- 下一篇: 《R语言实战》第3章