1. 向量 vector
向量是用于存儲數(shù)值型、字符型或邏輯型數(shù)據(jù)的一維數(shù)組。
#向量只包含一種數(shù)據(jù)類型
a <- c(1,2,5,3,6,-2,4)
b <- c("one","two","three")
c <- c(TRUE,TRUE,TRUE,FALSE,TRUE,TRUE)
#標(biāo)量 只含一個元素的向量,用于保存常量
d <- 3
#訪問向量元素
t <- c("k","j","h","a","c","m")
t[3]
t[c(1,3,5)]#從第1列開始,取第1,3,5列
t[2:6]#從第2列開始取到第6列,含第六列
結(jié)果:
> a
[1] 1 2 5 3 6 -2 4
> b
[1] "one" "two" "three"
> c
[1] TRUE TRUE TRUE FALSE TRUE TRUE
> t[3]
[1] "h"
> t[c(1,3,5)]#從第1列開始,取第1,3,5列
[1] "k" "h" "c"
> t[2:6]#從第2列開始取到第6列,含第六列
[1] "j" "h" "a" "c" "m"
2. 矩陣 matrix
矩陣是二維數(shù)組,每個元素數(shù)據(jù)類型相同。
#賦值1
y <- matrix(1:20,nrow = 5,ncol = 4)
y
#####賦值2###
cells <- c(1,26,24,68)
rnames <- c("R1","R2")
cnames <- c("C1","C2")
#按行賦值生成2X2矩陣
mymatrix <- matrix(cells,nrow = 2,ncol = 2,byrow = TRUE,
dimnames = list(rnames,cnames))
#按列賦值生成2X2矩陣
mymatrix <- matrix(cells,nrow = 2,ncol = 2,byrow = FALSE,
dimnames = list(rnames,cnames))
#矩陣取值
x <- matrix(1:10,nrow = 2)
x[2,]#第2行
x[,2]#第2列
x[1,4]#第1行第4列
x[1,c(4,5)]#第1行,4,5列
結(jié)果:
> 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
> #按行賦值生成2X2矩陣
> mymatrix
C1 C2
R1 1 26
R2 24 68
> #按列賦值生成2X2矩陣
> mymatrix
C1 C2
R1 1 24
R2 26 68
> x[2,]#第2行
[1] 2 4 6 8 10
> x[,2]#第2列
[1] 3 4
> x[1,4]#第1行第4列
[1] 7
> x[1,c(4,5)]#第1行,4,5列
[1] 7 9
3. 數(shù)組 array
數(shù)組與矩陣類似,可以多維。
dim1 <- c("A1","A2")
dim2 <- c("B1","B2","B3")
dim3 <- c("D1","D2","D3","D4")
q <- array(1:24,c(2,3,4),dimnames = list(dim1,dim2,dim3))
q
結(jié)果:
> q
, , D1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , D2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , D3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , D4
B1 B2 B3
A1 19 21 23
A2 20 22 24
4. 數(shù)據(jù)框 data.frame
數(shù)據(jù)框可以有多種數(shù)據(jù)模式。
id <- c(1,2,3,4)
age <- c(25,34,28,52)
types <- c("T1","T2","T1","T1")
status <- c("P","I","E","P")
data_id <- data.frame(id,age,types,status)
data_id
###選取數(shù)據(jù)框元素####
data_id[1:2]#從第1列開始取,包含第2列
data_id[c("id","age")]
data_id$types
#####交叉表####
table(data_id$types,data_id$status)
#####attach(),detach()設(shè)置R搜索路徑#########
summary(mtcars$mpg)
attach(mtcars)
summary(mtcars$mpg)
detach(mtcars)
#########with#######
with(mtcars,{
nook <- summary(mpg) #keep在with函數(shù)內(nèi)使用
keep <<- summary(mpg)#keep可在with函數(shù)外面使用
})
keep
結(jié)果
> data_id
id age types status
1 1 25 T1 P
2 2 34 T2 I
3 3 28 T1 E
4 4 52 T1 P
> data_id[1:2]#從第1列開始取,包含第2列
id age
1 1 25
2 2 34
3 3 28
4 4 52
> data_id[c("id","age")]
id age
1 1 25
2 2 34
3 3 28
4 4 52
> data_id$types
[1] "T1" "T2" "T1" "T1"
> table(data_id$types,data_id$status)
E I P
T1 1 0 2
T2 0 1 0
> attach(mtcars)
> summary(mtcars$mpg)
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90
> detach(mtcars)
> keep
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90
5. 列表 list
列表是一些對象的有序集合。
###list##
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
mylist[[3]]
mylist[["ages"]]
結(jié)果
> 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[[3]]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
> mylist[["ages"]]
[1] 25 26 18 39
注:參考書籍《R語言實戰(zhàn)》