Ashish
Ashish

Reputation: 177

Trying to make a simple frequency table in R

Hi I'm just starting out learning R. I'm trying to make a frequency table. My data is this

62 64 64 64 65 65 65 65 67 67 67 68 68 70 70 72 73 76 79 80

I want my frequency table to show all the numbers from 60 to 80. Numbers not represented in my data set will of course have a frequency of 0.

I'm able to get a nice frequency table with the table() function and I also turned it into a data frame and added in a proportion and percentage column but I don't know how to do it so that it includes the numbers with a frequency of 0.

Upvotes: 2

Views: 234

Answers (1)

akrun
akrun

Reputation: 887951

We can convert the vector to factor with levels specified from 60 to 80 and then get the frequency with table

table(factor(myvec, levels = 60:80))
# 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 
# 0  0  1  0  3  4  0  3  2  0  2  0  1  1  0  0  1  0  0  1  1 

data

myvec <- c(62, 64, 64, 64, 65, 65, 65, 65, 67, 67, 67, 68, 68, 70, 70, 
               72, 73, 76, 79, 80)

Upvotes: 3

Related Questions