This question already has an answer here:
I have the following data set:
sample.data <- data.frame(Step = c(1,2,3,4,1,2,1,2,3,1,1),
Case = c(1,1,1,1,2,2,3,3,3,4,5),
Decision = c("Referred","Referred","Referred","Approved","Referred","Declined","Referred","Referred","Declined","Approved","Declined"))
sample.data
Step Case Decision
1 1 1 Referred
2 2 1 Referred
3 3 1 Referred
4 4 1 Approved
5 1 2 Referred
6 2 2 Declined
7 1 3 Referred
8 2 3 Referred
9 3 3 Declined
10 1 4 Approved
11 1 5 Declined
Is it possible in R to translate this into a wide table format, with the decisions on the header, and the value of each cell being the count of the occurrence, for example:
Case Referred Approved Declined
1 3 1 0
2 1 0 1
3 2 0 1
4 0 1 0
5 0 0 1
You can accomplish this with a simple
table()
statement. You can play with setting factor levels to get your responses the way you want.We can use the
base R
xtabs
The aggregation parameter in the
dcast
function of thereshape2
-package defaults tolength
(= count). In thedata.table
-package an improved version of thedcast
function is implemented. So in your case this would be:or with using the parameters explicitly:
This gives the following dataframe:
If you don't specify an aggregation function, you get a warning telling you that
dcast
is usinglenght
as a default.Here's a dplyr + tidyr approach: