加载中…
个人资料
守株待兔
守株待兔
  • 博客等级:
  • 博客积分:0
  • 博客访问:0
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:大 中 小

次数分布

(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
> as.matrix(table(x))
   [,1]
30    3
31    2
32    8
33    5
34    8
35    1
36    4
37    3
38    2
39    3
40    1
41    2
42    5
43    5
45    1
46    1
47    3
48    1
49    7
50    4
51    5
52    1
53    3
54    3
55    3
56    2
57    5
58    4
59    4
60    1

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.matrix(table(factorx))
#Add cumFreq and proportions transform函数用来增加列,也可以改变列。
xout <- transform(xout, cumFreq = cumsum(xout[,1]), relative = prop.table(xout[,1]))
colnames(xout)[1]="freq"
xout

as.matrix和matrix的区别
如果是as.matrix,默认的行名无法改变,无法使用dimnames进行设置;matrix可以用dimnames进行设置。


下面也可以
x <- sample(30:60, 100, TRUE)
y<-table(cut(x, breaks=c(29,35,40,45,50,55,60),include.lowest=TRUE,right=FALSE))
cbind( Freq=y, Cumul=cumsum(y), relative=prop.table(y))


x<-c(70, 82, 91, 76, 84, 94, 72, 76, 74, 67, 70, 45, 69, 86, 75,
84, 63, 93, 87, 67, 71, 83, 90, 88, 58, 69, 91, 74, 80, 79, 
75, 88, 87, 50, 80, 84, 94, 72, 85, 71, 81, 74, 75, 81, 75, 57,
78, 78, 61, 60)                
y<-table(cut(x, breaks=c(0,60,70,80,90,100),include.lowest=TRUE,right=FALSE))
z<-cbind(y,"prop%"=prop.table(y)*100)
列名字是字符串。

0

阅读┊ 收藏 ┊ 喜欢▼ ┊打印┊举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有