Skip to contents

This function creates a table with the differences in turnout between multiple votes for every counting circle in the original data.

Usage

get_differences(df, comb1, comb2, geo_cols = c("gemwkid", "gemeinde"))

Arguments

df

A table containing the counting circle ID and voter turnout for various issues. Each column represents a specific issue, and the column names should correspond to the issue IDs (e.g., 'eidg1', 'kant2').

comb1, comb2

A character vector specifying the the first and second set of column to be compared. The column names represent columns in df that contain voter turnout data of the issues of interest (e.g., "eidg1", "kant2").

geo_cols

The name of the geo-column containing an identifier of the counting circle.

Value

A dataframe containing voter turnout differences between all combinations of vote issues defined.

Examples


testdata <- data.frame(
  gemwkid = c(13,49,41,43,44),
  eidg1 = c(60.90,61.18,65.27,55.36,57.68),
  eidg2 = c(62.16,62.54,66.95,56.65,58.68),
  kant1 = c(57.73,60.27,63.31,51.93,54.49)
)

# generate combinations
combinations <- as.data.frame(t(combn(c("eidg1", "eidg2", "kant1"), 2)))

# calculate all possible differences between columns
get_differences(testdata, combinations$V1, combinations$V2, "gemwkid")
#>    gemwkid combination difference
#> 1       13 eidg1_eidg2      -1.26
#> 2       49 eidg1_eidg2      -1.36
#> 3       41 eidg1_eidg2      -1.68
#> 4       43 eidg1_eidg2      -1.29
#> 5       44 eidg1_eidg2      -1.00
#> 6       13 eidg1_kant1       3.17
#> 7       49 eidg1_kant1       0.91
#> 8       41 eidg1_kant1       1.96
#> 9       43 eidg1_kant1       3.43
#> 10      44 eidg1_kant1       3.19
#> 11      13 eidg2_kant1       4.43
#> 12      49 eidg2_kant1       2.27
#> 13      41 eidg2_kant1       3.64
#> 14      43 eidg2_kant1       4.72
#> 15      44 eidg2_kant1       4.19