R基础数据类型-数组、因子、列表

数组

 

 

 

数组与矩阵类似,是矩阵的扩展,把数据的维度扩展到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

> cbind(f1, d4=rep("HAHA", times=5))

  d1 d2 d3   d4

1  A  M  1 HAHA

2  B  F  2 HAHA

3  C  M  3 HAHA

4  D  F  5 HAHA

5  A  M  0 HAHA

 
> names(f1)

[1] "d1" "d2" "d3"

 

 

> names(f1)[3]="D3"

> names(f1)

[1] "d1" "d2" "D3"

 

>

 

这里产生了一个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

 

> f5<-  factor(substring("statistics", 1:10, 1:10), levels = letters)

> f5

 [1] s t a t i s t i c s

Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

 

> f6 <- factor(LETTERS[4:1], order= TRUE)

> f6

[1] D C B A

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次,水平为truefalse

> 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" 

 

 

 

列表

 

> listtest1 <- list(a=c(1,2,3,4), b=c("A", "B", "C" , "V") , c=c(TRUE, FALSE), d=c(1+2i) )

> listtest1

$`a`

[1] 1 2 3 4

 

$b

[1] "A" "B" "C" "V"

 

$c

[1]  TRUE FALSE

 

$d

[1] 1+2i

 

> listtest1$a

[1] 1 2 3 4

> listtest1$d

[1] 1+2i

> listtest1$a[2]

[1] 2

> listtest1$d[1]

[1] 1+2i

> listtest1$c[3]

[1] NA

> listtest1$b[3]

[1] "C"

 
 

> summary(listtest1)

  Length Class  Mode    

a 4      -none- numeric 

b 4      -none- character

c 2      -none- logical 

d 1      -none- complex 

 

>

 

 

 

添加一个向量

> listtest2 <- c(listtest1, list(e=c(0,0,0,0), f=c("Y", "N")))

> listtest2

$`a`

[1] 1 2 3 4

 

$b

[1] "A" "B" "C" "V"

 

$c

[1]  TRUE FALSE

 

$d

[1] 1+2i

 

$e

[1] 0 0 0 0

 

$f

[1] "Y" "N"

 

> summary(listtest2)

  Length Class  Mode    

a 4      -none- numeric 

b 4      -none- character

c 2      -none- logical 

d 1      -none- complex 

e 4      -none- numeric 

f 2      -none- character

 

>

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/guduchangjian/article/details/85083672