1.向量數組
向量是用來存儲數值型、字符型或邏輯性數據的一維數組,用函數c()建立向量數據結構
a <- c(1,2,5,6,4)函數
b <- c("one","two","three") component
c <- c("TRUE","FALSE")對象
2.矩陣three
矩陣是一個二維數組,只是每一個元素都擁有相同的模式,用函數matrix()建立矩陣it
y <- matrix(1:20, nrow = 5, ncol = 4)變量
3.數組List
數組與矩陣相似,可是維度能夠大於2,用函數array()建立數組數據類型
dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3")
dim3 <- c("C1", "C2", "C3", "C4")
z <- array(1:24, c(2, 3, 4), dimnames=list(dim1, dim2, dim3))
4.數據框
數據框的概念較矩陣來講更爲通常,數據框是R中最常處理的數據結構,用函數data.frame()建立
patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
5.因子
類別(名義型)變量和有序類別(有序型)變量在R中稱爲因子(factor)。因子在R中非
常重要,由於它決定了數據的分析方式以及如何進行視覺呈現。
diabetes <- c("Type1", "Type2", "Type1", "Type1")
diabetes <- factor(diabetes)
6.列表
列表(list)是R的數據類型中最爲複雜的一種。通常來講,列表就是一些對象(或成分,
component)的有序集合。列表容許你整合若干(可能無關的)對象到單個對象名下。
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)