-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ab2c3b
commit 484f908
Showing
10 changed files
with
702 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
Package: vdemlite | ||
Title: What the Package Does (One Line, Title Case) | ||
Title: A Lightwight Tool for Querying V-Dem Data | ||
Version: 0.0.0.9000 | ||
Authors@R: | ||
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"), | ||
person("Emmanuel", "Teitelbaum", , "emmanuel.teitelbaum@gmail.com", role = c("aut", "cre"), | ||
comment = c(ORCID = "YOUR-ORCID-ID")) | ||
Description: What the package does (one paragraph). | ||
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a | ||
license | ||
Description: | ||
vdemlite provides provides users with the ability to query a slimmed doown version | ||
of the V-Dem dataset. The core function, fetchdem, allow users to specify indicators, | ||
time periods, and countries of interest, with options to return data in wide or long | ||
format. | ||
License: CC BY-SA 4.0 + file LICENSE | ||
URL: https://creativecommons.org/licenses/by-sa/4.0/ | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.1 | ||
Depends: | ||
R (>= 2.10) | ||
R (>= 4.3.0) | ||
LazyData: true | ||
Imports: | ||
dplyr, | ||
ggplot2, | ||
tidyr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Creative Commons Attribution-ShareAlike 4.0 International License | ||
|
||
This is a human-readable summary of (and not a substitute for) the license. | ||
|
||
You are free to: | ||
|
||
Share — copy and redistribute the material in any medium or format | ||
|
||
Adapt — remix, transform, and build upon the material for any purpose, even commercially. | ||
|
||
The licensor cannot revoke these freedoms as long as you follow the license terms. | ||
|
||
Under the following terms: | ||
|
||
Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. | ||
|
||
ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. | ||
|
||
No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. | ||
|
||
Notices: | ||
|
||
You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. | ||
|
||
No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(fetchdem) | ||
import(dplyr) | ||
import(tidyr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#' Fetch and Filter V-Dem Lite Data | ||
#' | ||
#' This function fetches and filters data from the V-Dem Lite dataset based on specified indicators, years, and countries. | ||
#' It can return the data in either wide or long format. | ||
#' | ||
#' @param indicators A character vector of indicator names or categories. If NULL, all indicators are included. | ||
#' @param start_year The starting year for filtering the data. Default is 1970. | ||
#' @param end_year The ending year for filtering the data. Default is 2023. | ||
#' @param countries A character vector of country IDs to filter the data. If NULL, all countries are included. | ||
#' @param wide Logical, whether to return the data in wide format. If FALSE, returns data in long format. Default is TRUE. | ||
#' @return A data frame containing the filtered V-Dem Lite data in the specified format. | ||
#' @import dplyr tidyr | ||
#' @examples | ||
#' fetchdem(indicators = c("v2x_polyarchy", "v2x_freexp_altinf"), start_year = 2000, end_year = 2020, countries = c("USA", "SWE"), wide = TRUE) | ||
#' fetchdem(indicators = "v2x_polyarchy", start_year = 1980, end_year = 2020, countries = NULL, wide = FALSE) | ||
#' @export | ||
fetchdem <- function(indicators = NULL, | ||
start_year = 1970, | ||
end_year = 2023, | ||
countries = NULL, | ||
wide = TRUE) { | ||
|
||
# Load lookup table | ||
lookup_tbl <- lookup_tbl | ||
|
||
if (!is.null(indicators)) { | ||
# Initialize expanded indicators | ||
expanded_indicators <- c() | ||
|
||
# Loop over each indicator to check if it is a group variable | ||
for (indicator in indicators) { | ||
if (indicator %in% lookup_tbl$ll_category) { | ||
ll_tags <- lookup_tbl |> | ||
dplyr::filter(ll_category == indicator) |> | ||
dplyr::pull(tag) | ||
expanded_indicators <- unique(c(expanded_indicators, ll_tags)) | ||
} else if (indicator %in% lookup_tbl$hl_category) { | ||
hl_tags <- lookup_tbl |> | ||
dplyr::filter(hl_category == indicator) |> | ||
dplyr::pull(tag) | ||
expanded_indicators <- unique(c(expanded_indicators, hl_tags)) | ||
} else if (indicator %in% lookup_tbl$level) { | ||
level_tags <- lookup_tbl |> | ||
dplyr::filter(level == indicator) |> | ||
dplyr::pull(tag) | ||
expanded_indicators <- unique(c(expanded_indicators, level_tags)) | ||
} else if (indicator %in% lookup_tbl$big_5) { | ||
big_5_tags <- lookup_tbl |> | ||
dplyr::filter(big_5 == indicator) |> | ||
dplyr::pull(tag) | ||
expanded_indicators <- unique(c(expanded_indicators, big_5_tags)) | ||
} else if (indicator %in% lookup_tbl$addl_category) { | ||
addl_tags <- lookup_tbl |> | ||
dplyr::filter(addl_category == indicator) |> | ||
dplyr::pull(tag) | ||
expanded_indicators <- unique(c(expanded_indicators, addl_tags)) | ||
} else { | ||
# If the indicator is not a group variable, add it directly | ||
expanded_indicators <- unique(c(expanded_indicators, indicator)) | ||
} | ||
} | ||
|
||
# Remove duplicates from the final indicators list | ||
indicators <- unique(expanded_indicators) | ||
} | ||
|
||
# Start with the dataset | ||
filtered_data <- vdemlite |> | ||
dplyr::filter(year >= start_year & year <= end_year) | ||
|
||
# Filter by countries if provided | ||
if (!is.null(countries)) { | ||
filtered_data <- filtered_data |> | ||
dplyr::filter(country_text_id %in% countries) | ||
} | ||
|
||
# Select indicators if provided | ||
if (!is.null(indicators)) { | ||
filtered_data <- filtered_data |> | ||
dplyr::select(c("country_name", | ||
"country_text_id", | ||
"year", | ||
all_of(indicators))) | ||
} | ||
|
||
# Reshape data to wide or long format based on the user's choice | ||
if (wide) { | ||
return(filtered_data) | ||
} else { | ||
long_data <- filtered_data |> | ||
tidyr::pivot_longer(cols = -c(country_name, country_text_id, year), | ||
names_to = "indicator", | ||
values_to = "value") | ||
return(long_data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
utils::globalVariables(c("ll_category", "tag", "hl_category", "level", "big_5", "addl_category", "vdemlite", "year", "country_text_id", "country_name", "all_of")) |
Binary file not shown.
Oops, something went wrong.