數據類型
向量c
一維數據,必須相同類型
> a <- c(1,3,5,7)
> a
[1] 1 3 5 7
#若輸入不同類型,則發生轉化
> a <- c(1,'123')
> a
[1] "1" "123"
#訪問向量中的值
> a <- c(1,3,5,7)
> a[4] #並不是像其他語言那樣,索引從0開始
[1] 7
> a[c(1,4)]
[1] 1 7
#切片
> a[1:3]
[1] 1 3 5
矩陣matrix
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
- nrow—行數
- ncol—列數
- byrow—是否按行進行填充
- dimnames—行名與列名
#創建
> y = matrix(5:24,nrow=4,ncol=5)
> y
[,1] [,2] [,3] [,4] [,5]
[1,] 5 9 13 17 21
[2,] 6 10 14 18 22
[3,] 7 11 15 19 23
[4,] 8 12 16 20 24
> a <- c(1,3,5,7)
> rnames <- c("R1", "R2")
> cnames <- c("C1", "C2")
> newMatrix <- matrix(a, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames,cnames))
> newMatrix
C1 C2
R1 1 3
R2 5 7
#訪問其中的值
> x <- matrix(1:20, nrow=4, ncol=5)
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 5 9 13 17
[2,] 2 6 10 14 18
[3,] 3 7 11 15 19
[4,] 4 8 12 16 20
> x[3,]
[1] 3 7 11 15 19
> x[2,3]
[1] 10
> x[,4]
[1] 13 14 15 16
數組array
array(data = NA, dim = length(data), dimnames = NULL)
允許多重維度
#創建3x2x4的多維數組
> dim1 <- c("A1", "A2", "A3")
> dim2 <- c("B1", "B2")
> dim3 <- c("C1", "C2", "C3", "C4")
> d <- array(1:24, c(3,2,4), dimnames=list(dim1,dim2,dim3))
> d
, , C1
B1 B2
A1 1 4
A2 2 5
A3 3 6
, , C2
B1 B2
A1 7 10
A2 8 11
A3 9 12
, , C3
B1 B2
A1 13 16
A2 14 17
A3 15 18
, , C4
B1 B2
A1 19 22
A2 20 23
A3 21 24
> d[1,2,3] #行,列,維度
[1] 16
數據框data.frame
每一列的類型必須相同
data.frame(…, row.names = NULL, check.rows = FALSE,
check.names = TRUE, fix.empty.names = TRUE,
stringsAsFactors = default.stringsAsFactors())
> ?data.frame
# 生成一個data.frmae
> patientID <- c(1,2,3,4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type3", "Type2")
> status <- c("poor", "Improved", "Excellent", "poor")
> patientsData <- data.frame(patientID, age, diabetes, status)
> patientsData
patientID age diabetes status
1 1 25 Type1 poor
2 2 34 Type2 Improved
3 3 28 Type3 Excellent
4 4 52 Type2 poor
# 切片
> patientsData[1:2]
patientID age
1 1 25
2 2 34
3 3 28
4 4 52
> patientsData[c("diabetes", "status")
+ ]
diabetes status
1 Type1 poor
2 Type2 Improved
3 Type3 Excellent
4 Type2 poor
# 利用attach可直接輸入列名顯示該列
> patientsData$age
[1] 25 34 28 52
> attach()
Error in attach() : 缺少參數"what",也沒有缺省值
> age
[1] 25 34 28 52
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
> mpg
錯誤: 找不到對象'mpg'
> attach(mtcars)
> mpg
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
[15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
[29] 15.8 19.7 15.0 21.4
> detach(mtcars)
> mpg
錯誤: 找不到對象'mpg'
> with(mtcars, {})
NULL
> with(mtcars, {
+ l <- mpg
+ l}
+ )
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2
[15] 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4
[29] 15.8 19.7 15.0 21.4
> diabetes
[1] "Type1" "Type2" "Type3" "Type2"
> diabetes <- factor(diabetes)
> diabetes
[1] Type1 Type2 Type3 Type2
Levels: Type1 Type2 Type3
列表list
不同類型可以結合在一起
> ?list
> g <- "My first list"
> h <- c(12, 45, 43, 90)
> j <- matrix(1:10, nrow=2)
> k <- c("one", "two", "three")
> mylist <- list(g, h, j, k)
> mylist
[[1]]
[1] "My first list"
[[2]]
[1] 12 45 43 90
[[3]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
[[4]]
[1] "one" "two" "three"
#訪問
> mylist[2]
[[1]]
[1] 12 45 43 90