Home > Net >  sum the number of occurences based on two columns with 3 levels of categories in R
sum the number of occurences based on two columns with 3 levels of categories in R

Time:08-05

I have two columns with categorical data, they are different categories both both with levels 0,1 and 2.

I want to sum the number of times the combinations occur, but the total count just takes the total sum of that column.

Groups:   dfMBG$datasetG.snelheid [3]
  dfMBG$datasetG.snelheid as.character(dfMBG$~ countMB
  <chr>                     <chr>                    <dbl>
1 0                         0                          153
2 0                         1                          153
3 0                         2                          153
4 1                         0                          153
5 1                         1                          153
6 1                         2                          153
7 2                         0                          153
8 2                         1                          153
9 2                         2                          153

I want it to look something like this.

Groups:   dfMBG$datasetG.snelheid [3]
  dfMBG$datasetG.snelheid  as.character(dfMBG$~ countMB
  <chr>                     <chr>                    <dbl>
1 0                         0                          12
2 0                         1                          15
3 0                         2                          45
4 1                         0                          12
5 1                         1                          15
6 1                         2                          28
7 2                         0                          4
8 2                         1                          17
9 2                         2                          5

the code that I used is this:

MBGcount<-dfMBG %>% rowwise(.) %>%
  group_by(dfMBG$datasetG.snelheid, as.character(dfMBG$datasetG.indicatie)) %>%
 summarise(countMB = sum(as.numeric(dfMBG$datasetG.verstoringsbron)))
MBGcount
dfMBG$dfMBG$datasetG.verstoringsbron

consists of a column with 1's.

Thank you for helping me!

CodePudding user response:

If you run this instead, does it achieve what you want?

dfMBG %>%
  group_by(datasetG.snelheid, datasetG.indicatie) %>%
  count()
  • Related