Use table
after converting your data to a factor
:
nominal.vals <- 0:9
x <- c(1, 1, 1, 0, 0, 3, 1, 3, 3)
table(factor(x, levels=nominal.vals))
# 0 1 2 3 4 5 6 7 8 9
# 2 4 0 3 0 0 0 0 0 0
Question
This seems to be pretty basic but I am stuck!
The nominal values are:
0 1 2 3 4 5 6 7 8 9
I import data from csv and a column looks like:
1 1 1 0 0 3 1 3 3
So, I want to calculate the frequencies of the nominal values and get something like:
0 1 2 3 4 5 6 7 8 9
2 4 0 3 0 0 0 0 0 0
The closest thing I've found is to use table but as expected it doesn't include the 0 frequencies.
Solution
Use table
after converting your data to a factor
:
nominal.vals <- 0:9
x <- c(1, 1, 1, 0, 0, 3, 1, 3, 3)
table(factor(x, levels=nominal.vals))
# 0 1 2 3 4 5 6 7 8 9
# 2 4 0 3 0 0 0 0 0 0