如何在R中创建一个表显示每年等于一定值的观测百分比的表?

发布于 2025-02-01 15:02:41 字数 520 浏览 3 评论 0原文

我正在使用一个时间序列数据集,涉及专制政权的反对程度。我包括了下面的数据示例。我想制作一个表,该表显示v2psoppaut的每年的国家百分比。有人可以告诉我该怎么做吗?我想制作一个可以保存为新的df的表。

structure(list(year = 1900:1905, COWcode = c(70L, 70L, 70L, 70L, 
70L, 70L), country_name = c("Mexico", "Mexico", "Mexico", "Mexico", 
"Mexico", "Mexico"), country_text_id = c("MEX", "MEX", "MEX", 
"MEX", "MEX", "MEX"), v2x_regime = c(0L, 0L, 0L, 0L, 0L, 0L), 
    v2psoppaut_ord = c(2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, 
6L), class = "data.frame")

I'm working with a time series dataset on levels of opposition in authoritarian regimes. I've included a sample of the data below. I would like to produce a table that displays the percentage of countries per year with a value of 1 for v2psoppaut. Could someone tell me how to go about doing this? I'd like to produce a table that I can save as a new df for plotting.

structure(list(year = 1900:1905, COWcode = c(70L, 70L, 70L, 70L, 
70L, 70L), country_name = c("Mexico", "Mexico", "Mexico", "Mexico", 
"Mexico", "Mexico"), country_text_id = c("MEX", "MEX", "MEX", 
"MEX", "MEX", "MEX"), v2x_regime = c(0L, 0L, 0L, 0L, 0L, 0L), 
    v2psoppaut_ord = c(2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, 
6L), class = "data.frame")

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

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

发布评论

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

评论(2

云胡 2025-02-08 15:02:41

尝试使用dplyrtidyverse按年对数据进行分组,然后通过获取v2psoppaut_ord等于等于的行总和来汇总(汇总)(汇总) 1除了n()函数,除以该组内的行总数(例如年)。将其保存到新的DF进行绘图。您将有两个值:年和auth,后者表示您指示的变量的价值为1的比例(乘以100以获取百分比)。不要忘记使用ungroup()将数据分组

library(tidyverse)

plot_df <- df %>%
  group_by(year) %>%
  summarize(auth = sum(v2psoppaut_ord == 1, na.rm = T) / n()) %>%
  ungroup()

Trying using dplyr from tidyverse to group your data by year, then summarize it (aggregate) by taking the sum of rows where v2psoppaut_ord is equal to 1 divided by the total number of rows within that group (e.g. year) with the n() function. Save that to a new df for plotting. You will have two values: year and auth, with the latter indicating the proportion (multiply by 100 to get percentage) of countries with a value of 1 for the variable you indicated. Don't forget to ungroup the data with ungroup()

library(tidyverse)

plot_df <- df %>%
  group_by(year) %>%
  summarize(auth = sum(v2psoppaut_ord == 1, na.rm = T) / n()) %>%
  ungroup()
我只土不豪 2025-02-08 15:02:41

假设当您按年份分组时,每个国家/强>只有一个观察结果,那么您可以做这样的事情:

df %>% 
  group_by(year) %>% 
  summarize(prop = sum(v2psoppaut_ord == 1)/n())

这里propv2psoppaut_ord == 1 <的比例。 /代码>在组中的行数中。如果小组中的行是国家,那么这将为您提供所需的东西。您的数据应该看起来像这样可以工作:

df <- data.frame(year = c(rep(1900,3),rep(1901,3),rep(1902,3)), 
                 country_name = c(rep(c("Mexico", "Canada", "US"),3)), 
                 v2psoppaut_ord = c(sample(1:4,9,replace = T)))

Assuming that when you group by year you only have one observation per country then you could do something like this:

df %>% 
  group_by(year) %>% 
  summarize(prop = sum(v2psoppaut_ord == 1)/n())

Here prop is the proportion of v2psoppaut_ord == 1 out of the number of rows in the group. If the rows in the group are the countries, then this would give you what you're looking for. Your data should look something like this for this to work:

df <- data.frame(year = c(rep(1900,3),rep(1901,3),rep(1902,3)), 
                 country_name = c(rep(c("Mexico", "Canada", "US"),3)), 
                 v2psoppaut_ord = c(sample(1:4,9,replace = T)))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文