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).
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 |
Numeric example:
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:
[1] 5 9 7 4 4 7 4 6 5 5 4 5 6 2 4 6 7 2 5 5
Values Freq
1 2 2
2 4 5
3 5 6
4 6 3
5 7 3
6 9 1
With the table()
and cut()
functions, you
can build a frequency table in R.
[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.
(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.
(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
Using the HairEyeColor data from the "datasets" package.
Sample rows from HairEyeColor as a dataframe:
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.
Hair
Black Brown Red Blond
108 286 71 127
Or:
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.
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.
Hair
Black Brown Red Blond
32 25 10 5
Relative frequency table:
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.
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.
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
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes