次数分布
(2012-12-23 12:40:21)
标签:
杂谈 |
分类: r |
检索词:hist cut table
函数,as.matrix和matrix的区别
列名字是字符串,命名时通常省略“”符号,如果需要表达复杂的字符串作为列名字,需要在""里面编辑。
关于次数分布
使用hist函数。
1.breaks是一个分位vector
2.这个breaks创造的区间,两头都是封闭的,最大值、最小值,分别属于第一组、最后一组。
3.right =TRUE 右侧区间封闭;right =FALSE 右侧区间开放
hist(x, breaks = c(0,60,90,100),right =FALSE)
x<-c(0,10,80,95,100)
age<-sample(c(30:60),100,replace=TRUE)
hist.info=invisible(hist(age,breaks=seq(30,60,by=5)))
x<-matrix(0,nrow=length(hist.info$counts),ncol=2,dimnames=list(c(1:length(hist.info$counts)),c("interval","counts")))
int<-c()
for (i in 2:length(hist.info$breaks)){
int[i-1]<-paste0(as.character(hist.info$breaks[i-1]),"~",as.character(hist.info$breaks[i]))}
x[,1]=int
x[,2]=hist.info$counts
x
使用cut函数
1.最小值必须使用include.lowest=TRUE,进行封闭;否则是开放的
2.最大值总是封闭的,属于最后一组。
3.right =TRUE 右侧区间封闭;right =FALSE 右侧区间开放
cut函数产生了区间。
table函数产生连续取值的分布。
如果需要某个区间类的次数分布,可以通过组合完成table(cut(x))
请看
x <- sample(30:60, 100, TRUE)
table(x)
as.matrix(table(x))
[,1]
30 1
31 2
32 3
33 1
34 6
36 7
37 1
38 4
39 1
40 5
41 3
42 4
43 3
44 6
45 3
46 4
47 6
48 2
49 6
50 2
51 2
52 2
53 4
54 3
55 6
56 2
57 5
58 1
59 4
60 1
如果使用cut,产生区间。
> x <- sample(30:60, 100, TRUE)
> table(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE))
[29,35) [35,40) [40,45) [45,50) [50,55) [55,60]
26
13
13
13
16
19
> as.matrix(table(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE)))
[,1]
[29,35) 26
[35,40) 13
[40,45) 13
[45,50) 13
[50,55) 16
[55,60] 19
cut,table都会产生rownames.
x <- sample(30:60, 100, TRUE)
#Your code
factorx <- factor(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE))
#
#Tabulate and turn into data.frame
xout <- as.data.frame(table(factorx))
#as.data.frame(table(x))会产生一个默认的Freq列。
#Add cumFreq and proportions transform函数用来增加列,也可以改变列。
xout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))
xout
as.data.frame(table(factorx))会产生一个默认列名字为Freq的列。
> as.data.frame(table(x))
x Freq
1
30 3
2
31 2
3
32 8
4
33 5
5
34 8
6
35 1
7
36 4
8
37 3
9
38 2
10 39 3
11 40 1
12 41 2
13 42 5
14 43 5
15 45 1
16 46 1
17 47 3
18 48 1
19 49 7
20 50 4
21 51 5
22 52 1
23 53 3
24 54 3
25 55 3
26 56 2
27 57 5
28 58 4
29 59 4
30 60 1
关于次数分布
使用hist函数。
1.breaks是一个分位vector
2.这个breaks创造的区间,两头都是封闭的,最大值、最小值,分别属于第一组、最后一组。
3.right =TRUE 右侧区间封闭;right =FALSE 右侧区间开放
hist(x, breaks = c(0,60,90,100),right =FALSE)
x<-c(0,10,80,95,100)
age<-sample(c(30:60),100,replace=TRUE)
hist.info=invisible(hist(age,breaks=seq(30,60,by=5)))
x<-matrix(0,nrow=length(hist.info$counts),ncol=2,dimnames=list(c(1:length(hist.info$counts)),c("interval","counts")))
int<-c()
for (i in 2:length(hist.info$breaks)){
x[,1]=int
x[,2]=hist.info$counts
x
使用cut函数
1.最小值必须使用include.lowest=TRUE,进行封闭;否则是开放的
2.最大值总是封闭的,属于最后一组。
3.right =TRUE 右侧区间封闭;right =FALSE 右侧区间开放
cut函数产生了区间。
table函数产生连续取值的分布。
如果需要某个区间类的次数分布,可以通过组合完成table(cut(x))
请看
x <- sample(30:60, 100, TRUE)
table(x)
as.matrix(table(x))
30
31
32
33
34
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
如果使用cut,产生区间。
> x <- sample(30:60, 100, TRUE)
> table(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE))
[29,35) [35,40) [40,45) [45,50) [50,55) [55,60]
> as.matrix(table(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE)))
[29,35)
[35,40)
[40,45)
[45,50)
[50,55)
[55,60]
cut,table都会产生rownames.
x <- sample(30:60, 100, TRUE)
#Your code
factorx <- factor(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE))
#
#Tabulate and turn into data.frame
xout <- as.data.frame(table(factorx))
#as.data.frame(table(x))会产生一个默认的Freq列。
#Add cumFreq and proportions transform函数用来增加列,也可以改变列。
xout <- transform(xout, cumFreq = cumsum(Freq), relative = prop.table(Freq))
xout
as.data.frame(table(factorx))会产生一个默认列名字为Freq的列。
> as.data.frame(table(x))
1
2
3
4
5
6
7
8
9
10 39
11 40
12 41
13 42
14 43
15 45
16 46
17 47
18 48
19 49
20 50
21 51
22 52
23 53
24 54
25 55
26 56
27 57
28 58
29 59
30 60

加载中…