我是靠谱客的博主 外向野狼,这篇文章主要介绍R的数据对象的初步概括,现在分享给大家,希望可以做个参考。

在R中,最基础的两个概念是对象和函数
下面介绍R中的数据对象
R的数据对象可以初步划分为数据类型相同的:如向量,因子factors,矩阵matrix,数组array
和数据类型不同的:如列表list和数据框DataFrame
通过Is(),objects()可以查看对象。
通过rm()可以移除对象。
对于向量,向量化是R相较于其他语言最优势莫过于它的向量化,向量化也是R中最有效率的。
example:

复制代码
1
2
x<-c(1,3,5,7,9) sqrt(x)

如果是c语言,就需要用for语句取出数据,再一个个的取平方根
如果是python也是用for语句一个个遍历
对于向量,一个向量里面的所有数据必须是同一数据格式,如果不是的话,就要进行强制转换,一般是将数值型数据转化为字符型
对于向量,可以创建空向量,向量内的数据也可以为NA(缺失值)
因子factor
因子代表着对象的类别,将相同的数据归为一个类别
example

复制代码
1
2
3
4
5
6
> g<-factor(c("man","woman","man","woman")) > g [1] man woman man woman Levels: man woman

levels代表的向量C的类型,只有man和woman
注意因子在R语言中是以数值类型表示,如”man”在R中的表示就是1,”woman”在R中表示就是2
在因子有个函数table()应用比较广泛,table可以统计因子各level的频数,也可以结合两个因子构成交叉表
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> table(g) g man woman 2 2 > factor(c("adult","children","children","children")) [1] adult children children children Levels: adult children > m<-factor(c("adult","children","children","children")) > table(g,m) m g adult children man 1 1 woman 0 2

还有margin.table 和prop.table两个函数作为table的统计函数也很重要
margin.table(data,n)
n有两个值,一和二,如果是1的话统计行的频数,如果是2的话统计列的频数
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> margin.table(g,m) Error in margin.table(g, m) : 'x'不是陈列 > t<-table(g.m) Error in table(g.m) : object 'g.m' not found > t<-table(g,m) > margin.table(t) [1] 4 > margin.table(t,1) g man woman 2 2 > margin.table(t,2) m adult children 1 3

prop.table(data,n)同样也是统计因子,只不过统计的是占比
n=1时候以行为标准,n=2时候以列为标准
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
> prop.table(t) m g adult children man 0.25 0.25 woman 0.00 0.50 > prop.table(t,1) m g adult children man 0.5 0.5 woman 0.0 1.0 > prop.table(t,2) m g adult children man 1.0000000 0.3333333 woman 0.0000000 0.6666667 >

序列
生成序列的方法有很多种,主干是生成有序数列和无序数列
有序序列的生成方法
第一种是向量法 c<-1:10000
第二种是用seq函数 seq(from=,to=,length=)其实seq中有很多默认参数,一般常用的就只有by(代表步长和length(长度)
第三种就是rep(data,n) rep是一种复制函数,能将n代表的复制次数
最后一种gl(k,n,lables=c( )) k代表的因子水平个数,n代表k重复个数,lables是因子的标签
终上四种方式都是有序的,序列里面的data都是有迹可循的
生成无序序列的方法
第一种,用rnorm(n,mean=x,sd=y) mean代表平均值,sd代表标准差
生成的是长度为n,平均值为x,标准差为y的随机序列
第二种,用rt(n,df=x)
生成的是自由度为x,长度为n的随机序列
example如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
> seq(-4,100,2) [1] -4 -2 0 2 4 6 8 10 12 14 16 18 [13] 20 22 24 26 28 30 32 34 36 38 40 42 [25] 44 46 48 50 52 54 56 58 60 62 64 66 [37] 68 70 72 74 76 78 80 82 84 86 88 90 [49] 92 94 96 98 100 > seq(from=1,to=5,length=4) [1] 1.000000 2.333333 3.666667 5.000000 > length(length=10,from=-4,by=0.5) Error in length(length = 10, from = -4, by = 0.5) : 3 arguments passed to 'length' which requires 1 > seq(length=10,from=-4,by=0.5) [1] -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 > rep("nihao",each=10) [1] "nihao" "nihao" "nihao" "nihao" "nihao" "nihao" [7] "nihao" "nihao" "nihao" "nihao" > gl(2,5) [1] 1 1 1 1 1 2 2 2 2 2 Levels: 1 2 > a<-gl(2,5) > type(a) Error in type(a) : could not find function "type" > class(a) [1] "factor" > a<-gl(2,5,labels = c("man","female")) > a [1] man man man man man female female [8] female female female Levels: man female > rnorm(5,mean = 10,sd=2) [1] 12.096249 11.499978 10.603317 8.949008 6.570919 > rt(5,df=100) [1] -0.7648056 -0.7490618 -2.6534510 -0.3552033 [5] -0.1059763

数据子集
[]是数据子集重要的概念,数据对象带有[]和数据对象不带[]返回的结果完全是不一样的
example

复制代码
1
2
3
4
5
6
7
8
> x<-c(-1,0,-2,3,4,8,-100) > x>0 [1] FALSE FALSE FALSE TRUE TRUE TRUE FALSE > x[x>0] [1] 3 4 8

在这个例子中,如果是x>0,返回的就是布尔类型的数据
但是如果输入x[x>0]返回的就是数值类型的数据
[]里面可以加很多条件来筛选符合条件的数据值
example

复制代码
1
2
3
4
5
6
7
8
9
> x[x>2|x<0] [1] -1 -2 3 4 8 -100 > x[x>0&x<5] [1] 3 4

有个比较重要的概率是”-“号,在python中,列表等数据对象中”-“号代表是从右向左进行索引,而在R中不同的是,”-“号代表除去该数值索引的所有数据
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> x[-1] [1] 0 -2 3 4 8 -100 > x[-(3:5)] [1] -1 0 8 -100 > x[-c(3,5,7)] [1] -1 0 3 8

还有一个重要的概念是name属性
我们可以给R中的数据对象赋予name属性
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
> length(x) [1] 7 > names(x)<-c("a","b","c","d","e","f","g") > x a b c d e f g -1 0 -2 3 4 8 -100 > x[a] a a a a a b b b b b -1 -1 -1 -1 -1 0 0 0 0 0 > x["a"] a -1 > x[c("a","e")] a e -1 4

然后是二维的数据对象,数组和矩阵
矩阵是数组的特殊形式具有两个维度
下面是矩阵生成的几种方法:
第一种是由向量转化为矩阵,即一维数据对象转化为二维数据对象,需要有dim函数
examle

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
> m<-(1:10) > m [1] 1 2 3 4 5 6 7 8 9 10 > ?dim > a<-dim(m)<-(2,5) Error: unexpected ',' in "a<-dim(m)<-(2," > a<-dim(m)<-c(2,5) > a [1] 2 5 > m [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 > dim(m) [1] 2 5

第二种方法是用matrix直接生成矩阵,有一个参数需要特别注意byrow一般默认值为byrow=FLASE 也就是说数据填满矩阵的方式是按照列填满的,但是我们可以通过设置byrow=TRUE使数据按照行填满矩阵
我们同样可以设置colname和rowname给矩阵添加属性
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> a<-matrix(seq(from=1,to=10,length=10),2,5) > a [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 > a<-matrix(seq(from=1,to=10,length=10),2,5,byrow=T) > colnames(a)<-c("dasheng","tq","miss","you","so") > rownames(a)<-c("nihao","bye")

然后是数组,矩阵是数组的特殊化,数组一般具有两个及以上的维度,具体的生成方法和矩阵相似
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
> a1<-array(seq(from=1,to=15,length=15),dim=c(1,3,5)) > a1 , , 1 [,1] [,2] [,3] [1,] 1 2 3 , , 2 [,1] [,2] [,3] [1,] 4 5 6 , , 3 [,1] [,2] [,3] [1,] 7 8 9 , , 4 [,1] [,2] [,3] [1,] 10 11 12 , , 5 [,1] [,2] [,3] [1,] 13 14 15 > a<-matrix(seq(from=1,to=10,length=10),dim=c(2,5),byrow=T) Error in matrix(seq(from = 1, to = 10, length = 10), dim = c(2, 5), byrow = T) : 'dimnames' must be a list > a<-matrix(seq(from=1,to=10,length=10),2,5,byrow=T)

下面介绍数据类型可以不同的两个数据对象,列表和数据框
在R中列表是挺重要的,因为它允许异质的数据类型,在列表中可以同时存在字符串类型,数值类型和布尔类型的数据
list的一个突出特点在于[]索引和[[]]索引返回的值不一样
[]还返回列表名,而[[]]只返回值
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> my.lst<-list(stud.id=34453,stud.name="John",stud.marks=c(14.3,12.0,15.0,190)) > my.lst $`id` [1] 1 $name [1] "John" $mark [1] 14.3 12.0 15.0 190.0 $new [1] 1 3 5 7 9 > my.lst[[1]] [1] 1 > > my.lst[1] $`id` [1] 1

list相较于其他数据对象还有一个特点在于它的”[Math Processing Error] " 符 号 , " ”是代表列表里面不同类的数据,可以直接通过”[Math Processing Error] " 符 号 索 引 , 来 获 取 值 , 也 可 以 直 接 改 值 , 也 可 以 通 过 " ”直接生成一个新的数据
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> my.lst $`stud.id` [1] 34453 $stud.name [1] "John" $stud.marks [1] 14.3 12.0 15.0 190.0 > my.lst$stud.id [1] 34453 > my.lst$stud.id<-1 > my.lst$stud.id [1] 1 > my.lst$new<-(seq(1,10,2)) > my.lst$new [1] 1 3 5 7 9

names也是list的属性,可以较方便的更改list的数据名
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
> names(my.lst) [1] "stud.id" "stud.name" "stud.marks" > names(my.lst)<-c(id,name,marks) Error: object 'name' not found > names(my.lst)<-c("id","name","mark") > names(my.lst) [1] "id" "name" "mark" >

再介绍c()函数和unlist()函数
c()函数将两个list直接连接在一起
unlist()取消list的属性,将数据直接化为向量形式
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> other<-list(age=1,sex="man") > c(other,my.lst) $`age` [1] 1 $sex [1] "man" $id [1] 1 $name [1] "John" $mark [1] 14.3 12.0 15.0 190.0 $new [1] 1 3 5 7 9 > unlist(other) age sex "1" "man"

最后的就是数据框了,数据框是允许数据类型不同的二维数据对象,和matrix的定义有些类似而有些又相反
数据框的创建和list的创建类似
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> my.dataset<-data.frame(site=c("A","B","A","A","B"), + season=c("Winter","Summer","Summer","Spring","Fall"), + PH=c(7.4,6.3,8.6,7.2,8.9),stringsAsFactors=F) > my.dataset site season PH 1 A Winter 7.4 2 B Summer 6.3 3 A Summer 8.6 4 A Spring 7.2 5 B Fall 8.9

按照条件筛选数据,通过subset函数
example

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
> subset(my.dataset,PH>8) site season PH 3 A Summer 8.6 5 B Fall 8.9 > subset(my.dataset,PH>8,site,PH) Error in `[.data.frame`(x, r, vars, drop = drop) : object 'PH' not found > subset(my.dataset,PH>8,"site","PH") Error in drop && length(x) == 1L : invalid 'x' type in 'x && y' > subset(my.dataset,PH>8,site:PH) site season PH 3 A Summer 8.6 5 B Fall 8.9 > subset(my.dataset,PH>8,season:PH) season PH 3 Summer 8.6 5 Fall 8.9

看names属性
example

复制代码
1
2
3
4
5
6
> colnames(my.dataset) [1] "site" "season" "PH" > rownames(my.dataset) [1] "1" "2" "3" "4" "5"

添加新列

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> my.dataset$new<-c(1,20,15,12,21) > my.dataset site season PH new 1 A Winter 7.4 1 2 B Summer 6.3 20 3 A Summer 8.6 15 4 A Spring 7.2 12 5 B Fall 8.9 21

看行数和列数
example

复制代码
1
2
3
4
> nrow(my.dataset) [1] 5 > ncol(my.dataset) [1] 4

这些就是R语言的数据对象和一些初步的函数
是R语言的基础,是更深入学习R的基石。写下文章来做复习和总结,希望自己以后能够熟练的掌握R语言

最后

以上就是外向野狼最近收集整理的关于R的数据对象的初步概括的全部内容,更多相关R内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部