matrix语法格式
mymatrix <- matrix(vector, nrow = number_of_rows, ncol = number_of_columns, byrow = logical_value, dimnames = list(char_vector_rownames, char_vector_colnames))
vector包含了矩阵的元素。
nrow和ncol用以指定行和列的维数。
dimnames包含了可选的、以字符型向量表示的行名和列名。
byrow则表明矩阵应当按行填充(byrow = TRUE)还是按列填充(byrow = FALSE)
示例
rnames <- c(“A1”,“A2”,“A3”)
cnames <- c(“一月”,“二月”,“三月”)
mymatrix <- matrix(1:9, nrow = 3, ncol = 3, byrow = FALSE, dimnames = list(rnames, cnames))
mymatrix
一月 二月 三月
A1 1 4 7
A2 2 5 8
A3 3 6 9
数组array语法格式:
myarray <- array(vector, dimensions, dimnames)
其中vector包含了数组中的数据
dimensions是一个数值型向量,给出了各个维度下标的最大值
dimnames是可选的、各维度名称标签的列表。
dim1 <- c(“A1”,“A2”)
dim2 <- c(“B1”,“B2”,“B3”)
dim3 <- c(“C1”,“C2”,“C3”,“C4”)
myarray <- array(1:50, c(2,3,4), dimnames = list(dim1, dim2, dim3))
myarray
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24
data.frame()语法格式:
mydata <- data.frame(col1, col2, col3, …)
patientID <- c(1,2,3,4)
age <- c(25,34,29,53)
diabetes <- c(“Type1”,“Type2”,“Type3”,“Type4”)
status <- c(“Poor”,“Improved”,“Excellent”,“Poor”)
patientdata <- data.frame(patientID, age, diabetes, status)
patientdata
patientID age diabetes status
1 1 25 Type1 Poor
2 2 34 Type2 Improved
3 3 29 Type3 Excellent
4 4 53 Type4 Poor
patientda