Kirsty N
Kirsty N

Reputation: 19

Frequency table in R

I'm battling with something that I think should be so simple! I have researched frequency tables in R and can't figure out what to do.

I have a data set with different questionnaires within this data set. I.e., The first questionnaire is made up of 10 items, and I have a separate column for each item/variable, i.e. Question 1, Question 2, Question 3 etc will each have it's own column.

Within each column, there are 400 responses (i.e., participants) where each participant gives a response of 0,1, or 2.

All I want to do, is generate a frequency count on all variables (i.e., Question 1, Question 2, Question 3 etc) so that I get a summary for all variables - for example:

"Question 1"

0 = 100

1 = 200

2 = 97

NA = 3

Total = 400

How do I generate this in a simple, neat table?

Upvotes: 0

Views: 2870

Answers (2)

Jeldrik
Jeldrik

Reputation: 611

The simplest way is indeed the solution of Y.Z. using table(), but a nicer way to do this is to use freq() from the package. It automatically shows you the percentages both including and excluding missing values.

If you want to create one big frequency table for all columns at once, you can check out this question: Create frequency tables for multiple factor columns in R

#install the package
install.packages("questionr")

#load the libraries
library(questionr) 

#Make some reproducible data 
set.seed(20)
colours <- sample(x = c("red", "green", "blue", NA), size = 76, replace = TRUE)

#Make a frequency table of the data
freq(colours)

       n    % val%
blue  11 14.5 17.5
green 25 32.9 39.7
red   27 35.5 42.9
NA    13 17.1   NA

Upvotes: 1

Y. Z.
Y. Z.

Reputation: 377

df   #the name of your raw table
table(df$columnName)   #This command should create the table for you

if you also want to see a total on the end, you can do this:

exampleTable <- table(df$columnName)
exampleTable <- cbind(exampleTable, Total = rowSums(exampleTable))

Upvotes: 2

Related Questions