Summarize an ordered factor
I have a column in a dataframe that contains an ordered factor. I summarize the number of entries in the column for each factor by melting the data, then casting it. So far, so good. But I need to include factors for which no rows exist, so that the summarized data shows all possible factors, not just the utilized ones.
The data frame:
> 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] 14开发者_开发问答913 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 ...
>
what I've done so far:
> df.melt <- melt(instats,id=c('status'),measure=c('incident'))
> cast(df.melt, status ~ .,length)
and I get:
status (all)
1 Resolved Pending Customer Action 11
2 Pending xxx Action 3
3 Pending yyy Action 7
4 Closed 54
what I want is:
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
I understand why the melting/casting gives me the results it does. But how else can I do this to get my desired results?
You could just use 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)
精彩评论