基于不同变量值的分类变量的频率表

发布于 2025-02-13 05:01:04 字数 915 浏览 1 评论 0原文

我有一个具有两个分类变量的数据框。第1列是变量1,第2列是变量2。我想创建一个频率表,当var2状态为1时,var1状态的次数为1、2和3。同样,当var2 status为2&amp时; 3,我想要var1状态1、2和3的频率。最后,我想在X轴上和y轴上绘制具有VAR2状态(1,2,3)的直方图每个VAR2状态。感谢您的帮助。

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"))

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"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-02-20 05:01:04

您可能正在寻找描述中的频率小号而不是直方图。

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")

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")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文