高级数据结构
数据框
创建数据框函数data.frame> x <- 10:1 > y <- -4:5 > q <- c("Hockey", "Football", "Baseball", "Curling", "Rugby", "Lacrosse", "Basketball", "Tennis", "Cricket", "Soccer") > theDF <- data.frame(x, y, q) > theDF x y q 1 10 -4 Hockey 2 9 -3 Football 3 8 -2 Baseball 4 7 -1 Curling 5 6 0 Rugby 6 5 1 Lacrosse 7 4 2 Basketball 8 3 3 Tennis 9 2 4 Cricket 10 1 5 Soccer
注意theDF中的名称也是变量,可以通过创建数据框中的过程来设定变量名字
> theDF <- data.frame(First=x, Second=y, Sport=q) > theDF First Second Sport 1 10 -4 Hockey 2 9 -3 Football 3 8 -2 Baseball 4 7 -1 Curling 5 6 0 Rugby 6 5 1 Lacrosse 7 4 2 Basketball 8 3 3 Tennis 9 2 4 Cricket 10 1 5 Soccer
nrow()得到数据框的行数
ncol()得到数据框的列数
dim()同时得到数据框的行数和列数
names()检查数据框的列名字
rownames()检查和指定数据框的行名字
> nrow(theDF) [1] 10 > ncol(theDF) [1] 3 > dim(theDF) [1] 10 3 > names(theDF) [1] "First" "Second" "Sport" > names(theDF)[3] [1] "Sport" > rownames(theDF) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > rownames(theDF) <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") > theDF First Second Sport one 10 -4 Hockey two 9 -3 Football three 8 -2 Baseball four 7 -1 Curling five 6 0 Rugby six 5 1 Lacrosse seven 4 2 Basketball eight 3 3 Tennis nine 2 4 Cricket ten 1 5 Soccer > rownames(theDF) [1] "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" > rownames(theDF) <- NULL > rownames(theDF) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
head()函数显示出前面几行
tail()函数显示出后面几行
> head(theDF) First Second Sport 1 10 -4 Hockey 2 9 -3 Football 3 8 -2 Baseball 4 7 -1 Curling 5 6 0 Rugby 6 5 1 Lacrosse > head(theDF, n=7) First Second Sport 1 10 -4 Hockey 2 9 -3 Football 3 8 -2 Baseball 4 7 -1 Curling 5 6 0 Rugby 6 5 1 Lacrosse 7 4 2 Basketball > tail(theDF) First Second Sport 5 6 0 Rugby 6 5 1 Lacrosse 7 4 2 Basketball 8 3 3 Tennis 9 2 4 Cricket 10 1 5 Soccer
访问单个数据
方法不同,返回的类型也不同,为了确保输出一个单列的数据框,有第三个参数:drop=FALSE
> theDF$Sport [1] Hockey Football Baseball Curling Rugby Lacrosse Basketball Tennis Cricket Soccer Levels: Baseball Basketball Cricket Curling Football Hockey Lacrosse Rugby Soccer Tennis > theDF[3, 2] [1] -2 > theDF[3, 2:3] Second Sport 3 -2 Baseball > theDF[c(3, 5), 2] [1] -2 0 > theDF[c(3, 5), 2:3] Second Sport 3 -2 Baseball 5 0 Rugby > theDF[, 3] [1] Hockey Football Baseball Curling Rugby Lacrosse Basketball Tennis Cricket Soccer Levels: Baseball Basketball Cricket Curling Football Hockey Lacrosse Rugby Soccer Tennis > theDF[, 2:3] Second Sport 1 -4 Hockey 2 -3 Football 3 -2 Baseball 4 -1 Curling 5 0 Rugby 6 1 Lacrosse 7 2 Basketball 8 3 Tennis 9 4 Cricket 10 5 Soccer > theDF[2, ] First Second Sport 2 9 -3 Football > theDF[, "Sport", drop=FALSE] Sport 1 Hockey 2 Football 3 Baseball 4 Curling 5 Rugby 6 Lacrosse 7 Basketball 8 Tennis 9 Cricket 10 Soccer
列表
通过list()函数创建> list(1, 2, 3) [[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 > list(c(1, 2, 3)) [[1]] [1] 1 2 3 > list3 <- list(c(1, 2, 3), 3:7) > list3 [[1]] [1] 1 2 3 [[2]] [1] 3 4 5 6 7 > list(theDF, 1:10) [[1]] First Second Sport 1 10 -4 Hockey 2 9 -3 Football 3 8 -2 Baseball 4 7 -1 Curling 5 6 0 Rugby 6 5 1 Lacrosse 7 4 2 Basketball 8 3 3 Tennis 9 2 4 Cricket 10 1 5 Soccer [[2]] [1] 1 2 3 4 5 6 7 8 9 10
可以使用names()函数查看或分配列表元素的名字
利用vector()创建一个具有一定长度的空列表
> emptyList <- vector(mode = "list", length = 4) > emptyList [[1]] NULL [[2]] NULL [[3]] NULL [[4]] NULL
矩阵
可以进行加减乘除等式运算,nrow, ncol, dim 也可使用
通过matrix()函数创建
> A <- matrix(1:10, nrow = 5) > A [,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8 [4,] 4 9 [5,] 5 10 > B <- matrix(21:30, nrow = 5) > B [,1] [,2] [1,] 21 26 [2,] 22 27 [3,] 23 28 [4,] 24 29 [5,] 25 30 > nrow(A) [1] 5 > ncol(A) [1] 2 > dim(A) [1] 5 2 > A+B [,1] [,2] [1,] 22 32 [2,] 24 34 [3,] 26 36 [4,] 28 38 [5,] 30 40 > A==B [,1] [,2] [1,] FALSE FALSE [2,] FALSE FALSE [3,] FALSE FALSE [4,] FALSE FALSE [5,] FALSE FALSE
矩阵乘法,要求左边矩阵的列数等于右边矩阵的行数,矩阵A,B都是5*2矩阵,所以为了能让B放在右边进行矩阵乘法运算,需要对矩阵B取转置
> A %*% t(B) [,1] [,2] [,3] [,4] [,5] [1,] 177 184 191 198 205 [2,] 224 233 242 251 260 [3,] 271 282 293 304 315 [4,] 318 331 344 357 370 [5,] 365 380 395 410 425
矩阵也有行,列名字
colnames()
rownames()
> colnames(A) NULL > rownames(A) NULL > colnames(A) <- c("Left", "Right") > rownames(A) <- c("1st", "2st", "3st", "4st", "5st") > colnames(B) NULL > rownames(B) NULL > colnames(B) <- c("First", "Second") > rownames(B) <- c("one", "two", "three", "four", "five")
对矩阵取转置进行矩阵乘法的时候,需要注意乘积结果,转置自然会置换行和列的名字,矩阵乘积的行名字保留左边矩阵的行名字,矩阵乘积的列名字保留右边矩阵的列名字。
> A %*% t(B) one two three four five 1st 177 184 191 198 205 2st 224 233 242 251 260 3st 271 282 293 304 315 4st 318 331 344 357 370 5st 365 380 395 410 425
数组
一个多维向量
通过array()函数创建,所有元素必须时相同类型第一个为行指标,第二个参数是列指标,剩下的参数代表其他维数
> theArray <- array(1:12, dim = c(2, 3, 2)) > theArray , , 1 [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 , , 2 [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12 > theArray[1,,] [,1] [,2] [1,] 1 7 [2,] 3 9 [3,] 5 11 > theArray[1,,1] [1] 1 3 5 > theArray[,,1] [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
数组和矩阵最主要的不同是,矩阵被限制为两维,二数组可以有任意维数
版权声明:本文为博主原创文章,未经博主允许不得转载。