Here, we show how to create frequency tables in R using the table(), xtabs() and cut() functions.

table() and cut() are from the "base" package, while xtabs() is from the "stats" package.

See also contingency tables (two-way frequency table).

Summary Functions for Building Frequency Tables in R
Function Usage
table() Build a frequency table for numbers and characters
xtabs() Build a frequency table from a dataframe
cut() Set break points for frequency table

1 Build a Simple Frequency Table in R

Numeric example:

# Enter the data by hand
Values = c(6, 8, 9, 7, 8, 8, 7, 9, 6, 10)

data.frame(table(Values))
  Values Freq
1      6    2
2      7    2
3      8    3
4      9    2
5     10    1

Character example:

# Enter the data by hand
Values = c("B", "A", "B", "B", "C",
           "A", "C", "C", "B", "A")

data.frame(table(Values))
  Values Freq
1      A    3
2      B    4
3      C    3

Simulated example:

set.seed(34)
Values = rbinom(20, 10, 0.5)
Values
 [1] 5 9 7 4 4 7 4 6 5 5 4 5 6 2 4 6 7 2 5 5
data.frame(table(Values))
  Values Freq
1      2    2
2      4    5
3      5    6
4      6    3
5      7    3
6      9    1

2 Build a Frequency Table with Breaks or Widths Set in R

With the table() and cut() functions, you can build a frequency table in R.

set.seed(123)
Values = rbinom(24, 8, 0.5)
Values
 [1] 3 5 4 6 6 2 4 6 4 4 6 4 5 4 2 6 3 2 3 6 6 5 5 7

Using quantiles:

You can set breaks to the quantile values by using the "breaks" argument and the "quantile" function.

table(cut(Values, breaks = quantile(Values)))

  (2,3.75] (3.75,4.5]    (4.5,6]      (6,7] 
         3          6         11          1 

Convert frequency table into dataframe:

You can convert a "cut" frequency table into a dataframe as follows.

ftable = table(cut(Values, breaks = quantile(Values)))
ftable = data.frame(ftable)
colnames(ftable) = c("Range", "Frequency")
ftable
       Range Frequency
1   (2,3.75]         3
2 (3.75,4.5]         6
3    (4.5,6]        11
4      (6,7]         1

Setting breaks:

You can set specific number of breaks by setting the "breaks" argument to a number.

# Set to 5 breaks.
table(cut(Values, breaks = 5))

(2,3] (3,4] (4,5] (5,6] (6,7] 
    6     6     4     7     1 
# Set specific break points from lowest to highest.
table(cut(Values, breaks = c(min(Values), 4, 6, 8, max(Values))))

(2,4] (4,6] (6,7] (7,8] 
    9    11     1     0 

3 Build a Frequency Table from a Dataframe in R

Using the HairEyeColor data from the "datasets" package.

Sample rows from HairEyeColor as a dataframe:

HC = data.frame(HairEyeColor)
HC
    Hair   Eye    Sex Freq
1  Black Brown   Male   32
6  Brown  Blue   Male   50
10 Brown Hazel   Male   25
13 Black Green   Male    3
16 Blond Green   Male    8
17 Black Brown Female   36
23   Red  Blue Female    7
27   Red Hazel Female    7
29 Black Green Female    2
32 Blond Green Female    8

Frequency table by a variable or factor:

This will aggregate the counts for each level of the chosen variable or factor.

xtabs(Freq ~ Hair, HC)
Hair
Black Brown   Red Blond 
  108   286    71   127 

Or:

freqt = xtabs(Freq ~ Hair, HC)
data.frame(freqt)
   Hair Freq
1 Black  108
2 Brown  286
3   Red   71
4 Blond  127

Frequency table excluding some factor levels:

You can exclude specific levels of a factor by adding the "exclude" argument.

xtabs(Freq ~ Hair,
      exclude = "Black", HC)
Hair
Brown   Red Blond 
  286    71   127 
# Exclude multiple levels of a factor in the table.
xtabs(Freq ~ Hair,
      exclude = c("Blond", "Brown"), HC)
Hair
Black   Red 
  108    71 

Frequency table subsetting the data:

You can subset the data, including only specific levels of factors by adding the "subset" argument.

xtabs(Freq ~ Hair,
      subset = Sex == "Male" & Eye == c("Brown", "Hazel"), HC)
Hair
Black Brown   Red Blond 
   32    25    10     5 

4 Relative Frequency Table (and with Percentages) in R

Relative frequency table:

Values = c(16, 18, 19, 17, 18, 18, 17, 19, 16, 10)
ft = data.frame(table(Values))
ft
  Values Freq
1     10    1
2     16    2
3     17    2
4     18    3
5     19    2

Return the relative frequency table by dividing each value by the sum of all values.

ft$Freq = ft$Freq/sum(ft$Freq)
ft
  Values Freq
1     10  0.1
2     16  0.2
3     17  0.2
4     18  0.3
5     19  0.2

Relative frequency table with percentages:

See the HC dataframe used here above.

ft = xtabs(Freq ~ Hair, HC)
ft = data.frame(ft)
ft
   Hair Freq
1 Black  108
2 Brown  286
3   Red   71
4 Blond  127

Return the frequency table with percentages by dividing each value by the sum of all values and multiplying by 100.

Percent = ft$Freq/sum(ft$Freq)*100
ft = data.frame(ft, Percent)
colnames(ft) = c("Hair", "Frequency", "Percentage")
ft
   Hair Frequency Percentage
1 Black       108   18.24324
2 Brown       286   48.31081
3   Red        71   11.99324
4 Blond       127   21.45270

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes