Home > Net >  Count number of times a value occurs and get the sum in R
Count number of times a value occurs and get the sum in R

Time:12-08

I have seen these questions have been answered multiple times before. However, my case is very unique and I need your input.

I've a data that looks like this

PipeID     price
125        100
125        200
456        300
523        400
523        500

I got the number of occurrence by PipeID using the following code

num_occur <- df %>%
   group_by(PipeID) %>%
   summarise(PipeID = sum(PipeID), freq = n())

This gives me the following results

PipeID    freq
125       2
456       1
523       2

However I want to find the sum of the frequency. For example, the sum of the number of times a frequency of 2 occurs. The desired output is as follows:

freq   count
2      4
1      1

Any timely help would be greatly appreciated!

CodePudding user response:

Does this solve your problem?

library(tidyverse)

df <- read.table(text = "PipeID     price
125        100
125        200
456        300
523        400
523        500",
header = TRUE)

df %>%
  group_by(PipeID) %>%
  summarise(PipeID = sum(PipeID),
            freq = n()) %>%
  group_by(freq) %>%
  summarise(count = sum(freq))
#> # A tibble: 2 × 2
#>    freq count
#>   <int> <int>
#> 1     1     1
#> 2     2     4

Created on 2022-12-08 with reprex v2.0.2

CodePudding user response:

You simplify a bit with

df %>%
  count(PipeID, name = "freq") %>%
  group_by(freq) %>%
  summarise(count = sum(freq))
#> # A tibble: 2 x 2
#>    freq count
#>   <int> <int>
#> 1     1     1
#> 2     2     4
  • Related