To generate summary statistics for factors in R, you can use various functions such as summary()
and table()
to obtain counts, proportions, and other descriptive statistics for each level of a factor. This is useful for understanding the distribution and frequency of categorical data.
In this example,
responses
which contains the values 'yes'
, 'no'
, and 'maybe'
. This vector represents different survey responses.factor()
function to convert the responses
vector into a factor. We assign the result to a variable named responses_factor
. The factor()
function automatically identifies the unique levels of the vector.summary()
function to generate summary statistics for the responses_factor
. The summary()
function provides a frequency count for each level of the factor.responses <- c('yes', 'no', 'maybe', 'yes', 'no', 'yes', 'maybe')
responses_factor <- factor(responses)
summary_responses <- summary(responses_factor)
print(summary_responses)
maybe no yes 2 2 3
In this example,
product_category
which contains the values 'Electronics'
, 'Clothing'
, and 'Furniture'
. This vector represents different product categories.factor()
function to convert the product_category
vector into a factor. We assign the result to a variable named product_category_factor
. The factor()
function automatically identifies the unique levels of the vector.summary()
function to generate summary statistics for the product_category_factor
. The summary()
function provides a frequency count for each level of the factor.product_category <- c('Electronics', 'Clothing', 'Clothing', 'Furniture', 'Electronics')
product_category_factor <- factor(product_category)
summary_product_category <- summary(product_category_factor)
print(summary_product_category)
Clothing Electronics Furniture 2 2 1
In this example,
satisfaction
which contains the values 'Satisfied'
, 'Neutral'
, and 'Dissatisfied'
. This vector represents different levels of customer satisfaction.factor()
function to convert the satisfaction
vector into a factor. We assign the result to a variable named satisfaction_factor
. The factor()
function automatically identifies the unique levels of the vector.summary()
function to generate summary statistics for the satisfaction_factor
. The summary()
function provides a frequency count for each level of the factor.satisfaction <- c('Satisfied', 'Neutral', 'Dissatisfied', 'Satisfied', 'Neutral', 'Dissatisfied', 'Satisfied')
satisfaction_factor <- factor(satisfaction)
summary_satisfaction <- summary(satisfaction_factor)
print(summary_satisfaction)
Dissatisfied Neutral Satisfied 2 2 3
In this tutorial, we learned How to Generate Summary Statistics for Factors in R language with well detailed examples.