2010-11-04 48 views
1

我在包含排序因子的数据框中有一列。我通过对数据进行融合,然后投射,总结每个因子列中的条目数量。到现在为止还挺好。但是我需要包含没有行的因素,以便汇总的数据显示所有可能的因素,而不仅仅是所利用的因素。总结一个排序因子

数据帧:

> str(instats) 
'data.frame': 75 obs. of 5 variables: 
$ incident  : Factor w/ 75 levels "INC000000503771",..: 1 2 3 4 5 6 7 8 9 10 ... 
$ submit.date :Class 'Date' num [1:75] 14907 14907 14907 14907 14907 ... 
$ resolved.date:Class 'Date' num [1:75] 14910 14907 14910 14907 14907 ... 
$ closed.date :Class 'Date' num [1:75] 14913 14910 14913 14910 14910 ... 
$ status  : Ord.factor w/ 6 levels "Opened"<"Resolved Pending Customer Action"<..: 5 5 5 5 5 5 5 5 5 5 ... 
> 

什么我迄今所做的:

> df.melt <- melt(instats,id=c('status'),measure=c('incident')) 
> cast(df.melt, status ~ .,length) 

,我也得到:

      status (all) 
1 Resolved Pending Customer Action 11 
2    Pending xxx Action  3 
3    Pending yyy Action  7 
4       Closed 54 

我要的是:

      status (all) 
1       Opened  0 
2 Resolved Pending Customer Action 11 
3    Pending xxx Action  3 
4    Pending yyy Action  7 
5       Closed 54 
6       Canceled  0 

我明白为什么熔化/铸造会给我它所做的结果。但我怎么能做到这一点,以获得我想要的结果?

回答

2

你可以只使用table

instats <- data.frame(status=sample(letters[1:5],75,TRUE)) 
instats$status <- factor(instats$status,levels=letters[1:6]) 

table(instats$status) 
as.data.frame(table(instats$status)) 

# or summary 
summary(instats$status) 
+0

完美。谢谢! – lightkeeper 2010-11-05 12:39:58