2

I have built a contingency table from a column in a dataframe:

> dat <- data.frame(x = c(1, 1, 3))
> table(dat)
x
1 3 
2 1

I want to add a "column" to the table for the missing factor levels (in this case "2") with a count of 0. The result should look like this:

> table(dat)
x
1 2 3 
2 0 1

I have searched the site and found many similar questions, but while they use the term "table" in the question title and body, they all actually ask about dataframes.

jay.sf
  • 60,139
  • 8
  • 53
  • 110

4 Answers4

2

You need to explicitly specify the levels, i.e.

table(factor(dat$x, levels = 1:3))

# 1 2 3
# 2 0 1
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
1

Since you seem to look for how the integer bins 1:max are filled, you could use tabulate instead.

tabulate(dat$x)
# [1] 2 0 1
jay.sf
  • 60,139
  • 8
  • 53
  • 110
1

When you create a factor variable, you can explicit all levels even the ones that do not appear in the data:

dat <- data.frame(x = c(1, 1, 3))

dat$x <- factor(dat$x, levels = 1:3)

table(dat)

Result:

x
1 2 3 
2 0 1 
pietrodito
  • 1,783
  • 15
  • 24
0

For completeness, using data.table one could do it like this. I included an example on numeric values as well as factors.

dat <- data.frame(
  x = c(1, 1, 3),
  y = factor(c(1, 1, 3), levels = 1:3),
  z = factor(c(1, 1, 3), levels = 1:3, labels = c("cat", "bird", "dog"))
)

# example with numeric values in column x
setDT(dat)[.(min(x):max(x)), .N, .EACHI, on = "x"]

#    x N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# example with factor column y
setDT(dat)[.(levels(y)), .N, .EACHI, on = "y"]

#    y N
# 1: 1 2
# 2: 2 0
# 3: 3 1

# same as above, a factor with labels
setDT(dat)[.(levels(z)), .N, .EACHI, on = "z"]

#       z N
# 1:  cat 2
# 2: bird 0
# 3:  dog 1
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22