-3
s_id    u_id    product stage   log date
2706771 369544  Auto    Page 2  03-06-2017 13:01
2706771 369544  Auto    Page 1  02-06-2017 15:51
2706771 369544  Auto    Page 2  02-06-2017 15:52
2903900 459574  Credit  Page 1  16-06-2017 17:14
2903900 459574  Credit  Page 2  16-06-2017 17:14
2903900 459574  Credit  Submit  16-06-2017 17:14
2903904 1879562 Person  Page 1  16-06-2017 17:14
2903904 1879562 Person  Page 2  16-06-2017 17:15
2903904 1879562 Person  Submit  16-06-2017 17:16
2903904 1879562 Person  Page 2  16-06-2017 17:16

My data frame is like above where I want to group by product and count of the stages for each products.

df%>%
  group_by(product) %>% summarise(a=count(stages))

I get an error saying-Evaluation error: no applicable method for 'groups' applied to an object of class "factor".

How do I apply count of factor for which have three or more levels(Stage-column) and my expected out table is below where all stage level become a header.

Product Page 1  Page 2  Submit
Auto    counts  counts  counts
Credit  counts  counts  counts
Person  counts  counts  counts
ekad
  • 14,436
  • 26
  • 44
  • 46
  • For future reference, I think the question as asked is an example of an xy problem. At the end you show what you want (known as a contingency table or cross tab or pivot table with counts), but you start with proposing a solution (group_by plus summarize), which is not designed to give you such data. You wand something like table(df$stages, df$product) or xtabs. – Elin Feb 19 '18 at 12:09

1 Answers1

-2

It will not get your table in 1 step but why don't you do something like this:

df %>%
group_by(product, stage) %>%
summarise(count = n())

That should get you the data which you can reformat as you like.

hank_044
  • 176
  • 6