1

I have a list of integers that are all between 1 and 365. There are some integers that appear multiple times and some that do not appear. I would like to use a function like count to have a dataframe that counts the number of occurrences it appears including if it does not appear.

df
x  freq
1  0
2  1
3  3
4  0

Currently, both the rows for 1 and 4 do not exist in my current count function df=count(list)

Andre
  • 123
  • 1
  • 13

1 Answers1

2

We can use factor with levels specified so that it will also take care of the missing elements and report the count as 0

table(factor(df$x, levels = 1:4))
akrun
  • 874,273
  • 37
  • 540
  • 662