简体   繁体   中英

Frequency table of a categorical variable based on a different variable value- R

I have a dataframe with two categorical variables. Column 1 is variable 1 and column 2 is variable 2. I want to create a frequency table with the number of times Var1 status is 1, 2 and 3 when Var2 status is 1. Similarly when Var2 status is 2 & 3, I want the frequency of Var1 status- 1, 2 and 3. At the end I want to plot a histogram with Var2 status (1,2,3) on x-axis and on y-axis a frequency of Var1 statuses for each of Var2 status. Thanks for the help.

structure(list(`1` = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1), `2` = c(3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2)), row.names = c(NA, -101L), class = c("tbl_df", "tbl", 
"data.frame"))

You are probably looking to plot barplot of frequency instead of histogram in your description.

library(tidyr)

# add column names for the dataframe example
names(df) <- paste0("Var", 1:2)

# group and summarise to find the number of occurrence for each paired Var2, Var1 combination
plotting_df <- df %>% group_by(Var2, Var1) %>% summarise(n=n())

# plot using the new summary data frame
ggplot(plotting_df, aes(x=factor(Var2), y=n, fill=factor(Var1))) +
  geom_col(position="dodge")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM