0

I am running some demographic data in R. I have created new variables which show what responses represent what demographic. Here is the example of the code I am running

data$race <- NULL
data$race [data$q1 == 1] <- "White"
data$race [data$q1 == 2] <- "Black / African American"
data$race [data$q1 == 3] <- "Hispanic / Latino"
data$race [data$q1 == 4] <- "Asian"
...

The problem I am encountering is that not every demographic appears in my data set. As such, when I run the table, some demographics don't show up.

    table (data$race)

White     Black / African American     Hispanic / Latino
100       100                          25               

I want to create a table that lists the empty demographics as zero.

    table (data$race)

White     Black / African American     Hispanic / Latino     Asian
100       100                          25                    0

I am not sure if there is an option in table to do this. Does anyone know of a way to code this? I tried exclude = FALSE, but that just creates an column. Is there a different option I can use?

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 4
    Does this answer your question? [Include levels of zero count in result of table()](https://stackoverflow.com/questions/1617061/include-levels-of-zero-count-in-result-of-table) Looks like you can set the _levels_ argument in the function to capture groups that have 0 records. `table(data$race, levels = c("White", "Black / African American", "Hispanic / Latino", "Asian"))` – OTStats Aug 16 '21 at 15:33
  • 2
    Alternatively you can make `race` a factor with `data$race <- factor(data$q1, labels = c("White", "Black / African American", "Hispanic / Latino", "Asian"))`. – dcarlson Aug 16 '21 at 17:15
  • The factor method works very well. I also use as.data.frame () to make the table easier to read. Thanks for your help. – Eric Boorman Dec 07 '21 at 18:26

0 Answers0