0

I have a dataset D2 containing total volume of issuances of a financial product by company by month.

  Company    month      Volume
   AWK    2013-01     24244000
   AWK    2013-02     12294000
   AWK    2013-03     17254000
   AWK    2013-04     13939000
   AWK    2013-05     38757000
   AWK    2013-06     34600000
   AWK    2013-08     25240000

I would like to filter out companies that have traded less than 8 months per year in average.

When a company has not traded on a specific month, D2 does not contain any information and D2$month goes directly to the next month.

A. Suliman
  • 12,923
  • 5
  • 24
  • 37
Romain Berrou
  • 27
  • 1
  • 7

1 Answers1

2

With dplyr you can summarize by year the times some firm appears and then filter by the condition you want, as following:

library(dplyr)
Data %>% 
        mutate(Count= 1)%>% 
        mutate(Year = substr(month,1,4))%>% group_by(Year, Company)%>%
        summarise(TotalByYear=sum(Count)) %>% filter(TotalByYear<8)
# A tibble: 1 x 3
# Groups:   Year [1]
  Year  Company TotalByYear
  <chr> <chr>         <dbl>
1 2013  AWK               7
Carles
  • 2,731
  • 14
  • 25