2

content table

I want to count all the O-V and G in the Kwaleitsbeoordeling column. And I want to group on the Branche and the Status.

df %>% 
group_by(Status) %>% 
Group
  summarize(mean_value = count(Kwaliteitsbeoordeling))

How do I summarise a string value? and group on 2 different columns?

Update:

expected result:

enter image description here

Data update:

Project ID
<dbl>
Branche
<chr>
Status
<chr>
HUB
<chr>
Rayonmanager
<chr>
Kwaliteitsbeoordeling
<chr>
Algemene_indruk
<chr>
Arbo
<chr>
aanvullende_dienstverlening
<chr>
1   Hotels  huidig  Utrecht Janssen O   V   O   V   
2   Overheid    opgezegd    Barendrecht Platter O   O   V   O   
3   Overheid    huidig  Amsterdam   Hartman V   V   G   V   
4   Onderwijs   opgezegd    Amsterdam   Verhoeven   V   O   G   N.V.T   
5   Food    huidig  Amsterdam   Hartman O   O   O   V   
6   Retail  opgezegd    Utrecht Janssen G   O   N.V.T   V   
7   Onderwijs   huidig  Eindhoven   Willems G   V   V   V   
8   Hotels  opgezegd    Barendrecht Velzeboer   G   V   V   G   
9   Food    huidig  Amsterdam   Hartman O   O   G   O   
10  Onderwijs   opgezegd    Barendrecht Platter V   G   O   N.V.T
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Count number of rows within each group](https://stackoverflow.com/questions/9809166/count-number-of-rows-within-each-group) – cardinal40 Mar 18 '20 at 18:13

1 Answers1

1

We can do a group by n() and then reshape to 'wide' format

library(dplyr)
library(tidyr)
df %>%
   group_by(Project_ID, Branche, Kwaliteitsbeoordeling) %>%
   summarise(n = n()) %>%
   pivot_wider(names_from = Kwaliteitsbeoordeling, values_from = n, 
         values_fill = list(n = 0))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you for this result, But when i do this code i'll get a mean number, and I want to see like 0= 4 times v= 5 times G = 8 times or somethign like that – ITStudentEnschede Mar 18 '20 at 18:11
  • @ITStudentEnschede if that is the case, you don't need the `group_by` and `summarise` in the first case. I got confused by your `mean_value`. In the second option, it would be `summarise(mean_value = n())` – akrun Mar 18 '20 at 18:12
  • @ITStudentEnschede can you add a dput of a small example so that I can test it. From the image, i cannot copy – akrun Mar 18 '20 at 18:51
  • How can I add some data? – ITStudentEnschede Mar 18 '20 at 19:08
  • @ITStudentEnschede you can do `dput(head(mtcars))` and get the output. similarly you can do `dput(head(yourdata))` change the number of rows so that it would make a reproducible example – akrun Mar 18 '20 at 19:08
  • @ITStudentEnschede it would make it easier with dput. anyway will try that. Reason is that it s not clear where the column delimiter ends – akrun Mar 18 '20 at 21:05
  • @ITStudentEnschede is `Status HUB` a single word – akrun Mar 18 '20 at 21:08
  • @ITStudentEnschede i updated the solution. May be that helps – akrun Mar 18 '20 at 21:12
  • 1
    Thank you very much – ITStudentEnschede Mar 18 '20 at 23:58