Home > Mobile >  Counting occurrences based on condition in R (using dplyr ?)
Counting occurrences based on condition in R (using dplyr ?)

Time:12-09


I have a dataset similar to this

Year ID Type
2000 A 0
2001 A 0
2002 A 1
2000 B 0
2001 B 0
2002 C 1
2003 C 1

I want to summarize, probably with dplyr, so that I get the count of type zero and type 1 per year.

Example:
Year Type Count
2000 0 2
2000 1 0

Thanks a lot, and sorry for not creating the appropriate table for visualization.

CodePudding user response:

I think this is what you want :

my_df <- my_df %>% group_by(Year, Type) %>% summarise(Count = n())

CodePudding user response:

If you want to see where count is 0 then you need a final complete too.

df <- structure(
  list(Year = c(2000L, 2001L, 2002L, 2000L, 2001L, 2002L,2003L),
       ID = c("A", "A", "A", "B", "B", "C", "C"), 
       Type = c(0L, 0L, 1L, 0L, 0L, 1L, 1L)
       ), 
  class = "data.frame", 
  row.names = c(NA,-7L)
  )

df %>%
  group_by(Year, Type) %>%
  count() %>%
  ungroup() %>%
  complete(Year, Type, fill = list(n = 0))

To return

   Year  Type     n
  <int> <int> <dbl>
1  2000     0     2
2  2000     1     0
3  2001     0     2
4  2001     1     0
5  2002     0     0
6  2002     1     2
7  2003     0     0
8  2003     1     1

CodePudding user response:

df <- structure(
  list(Year = c(2000, 2001, 2002, 2000, 2001, 2002,2003),
       Id = c("A", "A", "A", "B", "B", "C", "C"), 
       Type = c(0, 0, 1, 0, 0, 1, 1)
  ), 
  class = "data.frame", 
  row.names = c(NA,-7L)
)

Maybe this could help:

table(df$Year, df$Type)

      0 1
  2000 2 0
  2001 2 0
  2002 0 2
  2003 0 1
  • Related