Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.8 kB
2
Indexable

```{r}
#| echo: false
#| message: false

library(tidyverse)
library(dplyr)

data <- read.csv("v.dem.full.others.csv")

# 1: subset the data for european countries

european_countries <- subset(data, e_regiongeo %in% c(1, 2, 3, 4))

# 2: filter for the interwar period - honestly there isn't much point in this because for the table you need more years but idk

interwar_period <- subset(european_countries, year >= 1919 & year <= 1939)

# 3: stable autocracies: all that are not 1

stable_autocracies <- interwar_period |> 
  group_by(country_name) |> 
  filter(all(e_boix_regime != 1)) |> 
  distinct(country_name)

# 4: stable democracies: all that are 1

stable_democracies <- interwar_period |> 
  group_by(country_name) |> 
  filter(all(e_boix_regime == 1)) |> 
  distinct(country_name)

# 5: democracies that collapsed, so we have to check those who went from 1 to 0 using lag. lag is used to retrieve the value of a variable from a previous row within a group, so it basically checks for change. later is gonna ask for year of breakdown so it's easy to do it here. default is 0 because it works and if I write 1 it doesn't and honestly i am way to tired to try to figure out why.

democratic_breakdown <- interwar_period |>
  arrange(country_name, year) |>
  group_by(country_name) |>
  filter(e_boix_regime == 0 & lag(e_boix_regime, default = 0) == 1) |>
  summarize(year_of_breakdown = min(year))

# 5: new variable within the data of eu countries

data_newvariable <- european_countries |> 
  mutate(new_variable = case_when(
    country_name %in% stable_autocracies$country_name ~ 0,
    country_name %in% stable_democracies$country_name ~ 1,
    TRUE ~ NA_integer_
  )) |> 
  left_join(democratic_breakdown, by = "country_name") |>  #this is how i added the years
  mutate(new_variable = ifelse(!is.na(year_of_breakdown), 2, new_variable))


```

```{r}
#| echo: false
#| warning: false

# la fiesta de las libraries

library(tidyr)
library(knitr)
library(kableExtra)

custom_mean <- function(x) {
  if (any(!is.na(x))) {
    return(as.character(mean(as.numeric(x), na.rm = TRUE)))
  } else {
    return("No data available")
  }
}


# now the funny stuff. first the data for the new variable.

results_table <- data_newvariable |> 
  filter(country_name %in% unique(c(stable_autocracies$country_name, stable_democracies$country_name, democratic_breakdown$country_name))) |> 
  group_by(country_name) |> #otherwise it shows a thousand obs.
  summarize(
    `Democratic collapse` = ifelse(2 %in% new_variable, "Yes", "No"), #selfexplanatory: if there's 2 yes if there isnt no
    `Years democratic` = sum(e_boix_regime == 1 & year >= 1919 & year <= 1939, na.rm = TRUE), #sum
    `Year of democratic\nbreakdown` = ifelse(!is.na(year_of_breakdown), as.character(year_of_breakdown), "-"), #to show either the year or - if there wasnt collapse
    `Years democratic\n(1860-1913)` = sum(e_boix_regime == 1 & year >= 1860 & year <= 1913, na.rm = TRUE),
    `Avg value of V-Dem\npolyarchy\n(1900-1913)` = custom_mean(v2x_polyarchy[year >= 1900 & year <= 1913])) |> #for some reason it's showing NaN in some and i dont know how the fuck can i fix it
  distinct(country_name, .keep_all = TRUE) |> #so i dont have duplicate columns
  rename(Country = country_name)


# It didnt fit in the page this is how it fits the table horizontally with adjusted formatting and renamed columns

kable(results_table, caption = "Variation in the survival of democratic countries during the interwar period", 
      align = "c", format = "latex", booktabs = TRUE, longtable = TRUE) |> 
  kable_styling(latex_options = c("striped", "scale_down")) |> 
  landscape() |> 
  row_spec(0, bold = TRUE) |> 
  column_spec(1:6, width = "2.5cm") #this is to adjust the table to the margins of the page

```