数组
数组与矩阵类似,是矩阵的扩展,把数据的维度扩展到2个以上,
array(data = NA, dim = length(data), dimnames = NULL) as.array(x, ...) is.array(x) |
> dim1 <- c("A1", "A2", "A3") > dim2 <- c("B1", "B2", "B3", "B4", "B5") > dim3 <- c("C1", "C2") > array(c(1:30), dim = c(3,5,2), dimnames = list(dim1, dim2,dim3)) , , C1
B1 B2 B3 B4 B5 A1 1 4 7 10 13 A2 2 5 8 11 14 A3 3 6 9 12 15
, , C2
B1 B2 B3 B4 B5 A1 16 19 22 25 28 A2 17 20 23 26 29 A3 18 21 24 27 30
> a1<-array(c(1:30), dim = c(3,5,2), dimnames = list(dim1, dim2,dim3)) > is.array(a1) [1] TRUE > a1[A1, B2,C1] Error: object 'A1' not found > a1["A1",,] C1 C2 B1 1 16 B2 4 19 B3 7 22 B4 10 25 B5 13 28
|
数据框
dataframe
数据框是仅次于向量的最重要的数据对象类型
创建数据框
data.frame(..., row.names = NULL, check.rows = FALSE, check.names = TRUE, fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors())
|
data.frame(col1, col2, col3, ……)
> d1 <- c("A", "B", "C", "D") > d2 <- c("M", "F", "M", "F") > d3 <- c(1, 2, 3, 5) > data.frame(d1, d2, d3) d1 d2 d3 1 A M 1 2 B F 2 3 C M 3 4 D F 5 > f1 <-data.frame(d1, d2, d3) > f1 d1 d2 d3 1 A M 1 2 B F 2 3 C M 3 4 D F 5 > class(f1) [1] "data.frame" > f1["d1"] d1 1 A 2 B 3 C 4 D > f1["d3"] d3 1 1 2 2 3 3 4 5 > f1["1"] Error in `[.data.frame`(f1, "1") : undefined columns selected > f1[1,] d1 d2 d3 1 A M 1 > f1[-1,] d1 d2 d3 2 B F 2 3 C M 3 4 D F 5
|
dataframe 访问行时用 f1[1, ] 来访问第一行
用负号来排除第一行
直接用列明来访问列
对于dataframe的编辑有2中
rbind cbind
添加行,添加列
names修改数据列明
> d1 [1] "A" "B" "C" "D" > d2 [1] "M" "F" "M" "F" > d3 [1] 1 2 3 5 > f1 <- data.frame(d1,d2,d3) > f1 d1 d2 d3 1 A M 1 2 B F 2 3 C M 3 4 D F 5 > rbind(f1, list("E", "M", 0)) d1 d2 d3 1 A M 1 2 B F 2 3 C M 3 4 D F 5 5 <NA> M 0 Warning message: In `[<-.factor`(`*tmp*`, ri, value = "E") : 因子层次有错,产生了NA > typeof(d1) [1] "character" > mode(d1) [1] "character" > class(d1) [1] "character" > rbind(f1, list("A", "M", 0)) d1 d2 d3 1 A M 1 2 B F 2 3 C M 3 4 D F 5 5 A M 0
|
这里产生了一个warning message,是因为
你的第一个变量是factor类型,增加的那一行观测对应的值不在原level里。可能需要先将第一个factor类型转变成character类型
因子在后面再讲
d1 加字母E是加不进去的,但是加字母ABCD都可以,就是这个原因
修改dataframe的列名 用names来修改
也是用names来查看dataframe的列名
因子
创建因子
创建因子使用factor函数
factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA)
ordered(x, ...) is.factor(x) is.ordered(x) as.factor(x) as.ordered(x)
addNA(x, ifany = FALSE)
|
> f2 <- factor(c("A", "B", "C", "D")) > f2 [1] A B C D Levels: A B C D > f3 <- factor(c("A", "B", "A", "D", "C", "B")) > f3 [1] A B A D C B Levels: A B C D
|
上面是通过factor创建因子的
下面用gl函数创建因子序列
gl(n, k, length = n*k, labels = seq_len(n), ordered = FALSE)
|
n 表示因子水平的个数
k表示每个因子水平重复的次数
length =n*k 表示序列的长度
labels 一个n维向量,表示因子水平
ordered 一个逻辑值,为TRUE表示有序因子,FALSE为无序因子
生成水平数为3, 每个水平重复3次的因子序列 > gl(3,3) [1] 1 1 1 2 2 2 3 3 3 Levels: 1 2 3
两个因子水平,每个重复3次,水平为true和false > gl(2,3, labels=c("TRUE", "FALSE")) [1] TRUE TRUE TRUE FALSE FALSE FALSE Levels: TRUE FALSE
> gl(2,1, 10) [1] 1 2 1 2 1 2 1 2 1 2 Levels: 1 2
|
总结:
对于创建因子的函数有2个
一个是factor,一个是gl
因子的存储方式
R中因子都是以整形向量存储的。每个因子水平,对应一个整型数字
对于字符型创建的向量因子,会按照字母顺序排序,在对应到整数型向量中。
> status <- c("Poor", "Improved", "Excellent", "Poor") > class(status) [1] "character" > states.factor <- factor(status, order=TRUE) > class(states.factor) [1] "ordered" "factor" > states.factor1 <- factor(status) > class(states.factor1) [1] "factor" > storage.mode(states.factor) [1] "integer" > storage.mode(states.factor1) [1] "integer" > as.numeric(states.factor) [1] 3 2 1 3 > as.numeric(states.factor1) [1] 3 2 1 3 > levels(states.factor) [1] "Excellent" "Improved" "Poor" > levels(states.factor1) [1] "Excellent" "Improved" "Poor"
|
列表
添加一个向量
|