Lin - 1 year ago 58

R Question

I wanted to create a vector of counts if possible.

For example: I have a vector

`x <- c(3, 0, 2, 0, 0)`

How can I create a frequency vector for all integers between 0 and 3? Ideally I wanted to get a vector like this:

`> 3 0 1 1`

which gives me the counts of 0, 1, 2, and 3 respectively.

Much appreciated!

Answer Source

You can do

```
table(factor(x, levels=0:3))
```

Simply using `table(x)`

is not enough.