0

this is an R related question.

I have a variable with a series of category scores (i.e. "1 1 0", "1 1 0","1 0 1","1 1 1","1 1 0","0 0 0"). It is easy enough to use Tables or any of the similar alternatives to get a count of entries for each category: "0 0 0" = 1, "1 1 0" = 3, "1 0 1" = 1, "1 1 1" = 1.

My problem is that the data are missing some of the possible categories (i.e. "0 0 1","1 0 1","1 0 0","0 1 1"). I want to know if there is some way to count the number of each category entries in a variable that is missing some of the possible categories and produce a summary table that also includes the categories that were missing. For example: "0 0 0" = 1, "0 0 1" = 0, "0 1 0" = 0, "1 0 0" = 0, "1 1 0" = 3, "1 0 1" = 1, "0 1 1" = 0, "1 1 1" = 1.

I hope I have explained this well enough. Thanks in advance for any suggestions.

MikeS
  • 1
  • Please include some of rows of your data as sample reproducible example. – AnilGoyal Mar 17 '21 at 09:05
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 17 '21 at 09:24

1 Answers1

0

If you use table of a factor it will give also 0 for levels not observed.

x <- factor(1:3, levels=1:4)
table(x)
#x
#1 2 3 4 
#1 1 1 0 
GKi
  • 37,245
  • 2
  • 26
  • 48