案例一:8只鸟的4中形态参数
首先,数据输入
> Wingcrd <- c(59,55,53.5,55,52.5,57.5,53,55)
> Tarsus <- c(22.3,19.7,20.8,20.3,20.8,21.5,20.6,21.5)
> Head <- c(31.2,30.4,30.6,30.3,30.3,30.8,32.5,NA)
> Wt <- c(9.5,13.8,14.8,15.2,15.5,15.6,15.6,15.7)
> BirdData <- c(Wingcrd, Tarsus, Head, Wt)
> BirdData
[1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0 22.3 19.7 20.8 20.3 20.8
[14] 21.5 20.6 21.5 31.2 30.4 30.6 30.3 30.3 30.8 32.5 NA 9.5 13.8
[27] 14.8 15.2 15.5 15.6 15.6 15.7
//BirdData是一个长度为32(4×8)的单个向量
//符号[1]、[14]、[28]表示新的一行的第一个元素的索引编号,
//根据电脑显示器大小的不同,这些编号显示不同。
//Id向量在一行显示更多的数字,这里的作用是指出具有相似Id值的观察值属于同一种形态变量。
> Id <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4)
> Id
[1] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
//查看1:4命令的作用
> 1:4
[1] 1 2 3 4
//先产生一个VarNames的新变量,包含前面所提到的4个形态参数:
> VarNames <- c("Wingcrd", "Tarsus","Head", "Wt")
> VarNames
[1] "Wingcrd" "Tarsus" "Head" "Wt"
//利用rep函数生成所需要的向量,Id2是一个被赋予顺序的名字的字符串
> Id2 <- rep(VarNames, each = 8)
> Id2
[1] "Wingcrd" "Wingcrd" "Wingcrd" "Wingcrd" "Wingcrd" "Wingcrd"
[7] "Wingcrd" "Wingcrd" "Tarsus" "Tarsus" "Tarsus" "Tarsus"
[13] "Tarsus" "Tarsus" "Tarsus" "Tarsus" "Head" "Head"
[19] "Head" "Head" "Head" "Head" "Head" "Head"
[25] "Wt" "Wt" "Wt" "Wt" "Wt" "Wt"
[31] "Wt" "Wt"
//cbind函数将所结合的变量以列的形式输出。
> Z <- cbind(Wingcrd,Tarsus,Head,Wt)
> Z
Wingcrd Tarsus Head Wt
[1,] 59.0 22.3 31.2 9.5
[2,] 55.0 19.7 30.4 13.8
[3,] 53.5 20.8 30.6 14.8
[4,] 55.0 20.3 30.3 15.2
[5,] 52.5 20.8 30.3 15.5
[6,] 57.5 21.5 30.8 15.6
[7,] 53.0 20.6 32.5 15.6
[8,] 55.0 21.5 NA 15.7
//访问Z第一列
> Z[, 1]
[1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0
//同样是访问第一列
> Z[1 : 8, 1]
[1] 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0
//访问第二行
> Z[2, ]
Wingcrd Tarsus Head Wt
55.0 19.7 30.4 13.8
//访问第一只鸟的Windcrd值
> Z[1,1]
Wingcrd
59
//第二列和第三列的全部数据
> Z[,2:3]
Tarsus Head
[1,] 22.3 31.2
[2,] 19.7 30.4
[3,] 20.8 30.6
[4,] 20.3 30.3
[5,] 20.8 30.3
[6,] 21.5 30.8
[7,] 20.6 32.5
[8,] 21.5 NA
//第四只鸟的Wt值
> X <- Z[4,4]
> X
Wt
15.2
//所有鸟的Wt值
> Y <- Z[, 4]
> Y
[1] 9.5 13.8 14.8 15.2 15.5 15.6 15.6 15.7
除了Head值之外的所有数据
> W <-Z[, -3]
> W
Wingcrd Tarsus Wt
[1,] 59.0 22.3 9.5
[2,] 55.0 19.7 13.8
[3,] 53.5 20.8 14.8
[4,] 55.0 20.3 15.2
[5,] 52.5 20.8 15.5
[6,] 57.5 21.5 15.6
[7,] 53.0 20.6 15.6
[8,] 55.0 21.5 15.7
//D包含Z中第一、三、四列的数据
> D <- Z[,c(1, 3, 4)]
> D
Wingcrd Head Wt
[1,] 59.0 31.2 9.5
[2,] 55.0 30.4 13.8
[3,] 53.5 30.6 14.8
[4,] 55.0 30.3 15.2
[5,] 52.5 30.3 15.5
[6,] 57.5 30.8 15.6
[7,] 53.0 32.5 15.6
[8,] 55.0 NA 15.7
//E包含除去第一、三列的数据
> E <- Z[, c(-1, -3)]
> E
Tarsus Wt
[1,] 22.3 9.5
[2,] 19.7 13.8
[3,] 20.8 14.8
[4,] 20.3 15.2
[5,] 20.8 15.5
[6,] 21.5 15.6
[7,] 20.6 15.6
[8,] 21.5 15.7
//Z的维数
> dim(Z)
[1] 8 4
//dim函数输出存储起来
> n <-dim (Z)
> n
[1] 8 4
//进存储Z的行数
> nrow <- dim(Z)[1]
> nrow
[1] 8
//rbind函数将数据以行进行结合
> Z2 <-rbind(Wingcrd, Tarsus,Head,Wt)
> Z2
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
Wingcrd 59.0 55.0 53.5 55.0 52.5 57.5 53.0 55.0
Tarsus 22.3 19.7 20.8 20.3 20.8 21.5 20.6 21.5
Head 31.2 30.4 30.6 30.3 30.3 30.8 32.5 NA
Wt 9.5 13.8 14.8 15.2 15.5 15.6 15.6 15.7
截图
|
请发表评论