Let’s create a sample data frame
1 2 3 4 5 6 7 8 |
Name <- c("John", "Max", "Nancy", "Paul", "Sandra") Gender <- c("M","M","F","M","F") Fav_Col <- c("Red", "Green", "Blue", "Red", "Red") Eye_Col <- c("Brown", "Blue", "Green","Green", "Green") Smart_Phone <- c("Apple", "Samsung", "Apple", "Apple", "Google") data <- data.frame(Name,Gender,Fav_Col,Eye_Col,Smart_Phone) data$Name <- as.character(data$Name) |
1 |
data |
1 2 3 4 5 6 7 |
> data Name Gender Fav_Col Eye_Col Smart_Phone 1 John M Red Brown Apple 2 Max M Green Blue Samsung 3 Nancy F Blue Green Apple 4 Paul M Red Green Apple 5 Sandra F Red Green Google |
There are 4 Categorical variables in the data. table() can quickly count (frequency) the variables. However, we have to write table(data$column) as many times as we want to see. Let’s create a simple for loop to do the work.
1 2 3 4 5 |
for(i in names(data)) { if(class(data[[i]]) == "factor"){ print(table(data[[i]])) } } |
1 2 3 4 5 6 7 8 9 10 11 |
F M 2 3 Blue Green Red 1 1 3 Blue Brown Green 1 1 3 Apple Google Samsung 3 1 1 |
Yep… that is quick and easy way to glance at the Categorical variables.