如何在R中创建一个表显示每年等于一定值的观测百分比的表?
我正在使用一个时间序列数据集,涉及专制政权的反对程度。我包括了下面的数据示例。我想制作一个表,该表显示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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
dplyr
从tidyverse
按年对数据进行分组,然后通过获取v2psoppaut_ord
等于等于的行总和来汇总(汇总)(汇总) 1除了n()
函数,除以该组内的行总数(例如年)。将其保存到新的DF进行绘图。您将有两个值:年和auth,后者表示您指示的变量的价值为1的比例(乘以100以获取百分比)。不要忘记使用ungroup()
将数据分组Trying using
dplyr
fromtidyverse
to group your data by year, then summarize it (aggregate) by taking the sum of rows wherev2psoppaut_ord
is equal to 1 divided by the total number of rows within that group (e.g. year) with then()
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 withungroup()
假设当您按年份分组时,每个国家/强>只有一个观察结果,那么您可以做这样的事情:
这里
prop
是v2psoppaut_ord == 1 <的比例。 /代码>在组中的行数中。如果小组中的行是国家,那么这将为您提供所需的东西。您的数据应该看起来像这样可以工作:
Assuming that when you group by year you only have one observation per country then you could do something like this:
Here
prop
is the proportion ofv2psoppaut_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: