'How can I create a frequency table for a categorical variable?
I'm new to R and am working with a dataset that involves the favourite colours of people collected in a survey.
I simply have a list of responses with colours being repeated, and so I want to make a frequency table I can work from (to create stacked bar plots, or pie charts).
I tried just using the 'table' function but I was unable to use the created table any further when it came to making plots.
Here is an example of the data: mostFav
1 Blue
2 Red
3 Red
4 Black
5 Blue
6 Black
7 Purple
8 Blue
9 Orange
10 White
11 Green
12 Green
13 Blue
14 Blue
15 Blue
16 Blue
17 Brown
18 Blue
19 Blue
20 Black
Solution 1:[1]
Depending on what you want to do, you can also try using ggplot without transforming the data into a table first.
mostFav <- data.frame("color" = c('Blue', 'Red', 'Red', 'Black', 'Blue', 'Black', 'Purple', 'Blue', 'Orange', 'White', 'Green', 'Green', 'Blue', 'Blue', 'Blue', 'Blue', 'Brown', 'Blue', 'Blue', 'Black'))
library(ggplot2)
ggplot(data = mostFav, aes(x = color)) +
geom_bar()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Riiiiiibs |

