Skip to content
Snippets Groups Projects
Commit e4198f28 authored by gdutz's avatar gdutz
Browse files

Change first argument of leo_svydesign() to "data"

parent 1c4cb229
No related branches found
No related tags found
No related merge requests found
Package: leo
Type: Package
Title: Useful functions for LEO surveys
Version: 0.2.5
Version: 0.2.6
Author: Gregor Dutz
Maintainer: Gregor Dutz <gregor.dutz@uni-hamburg.de>
Description: Implements simple functions to handle data from two German surveys
......
#' Creates survey design objects for LEO 2010 and 2018.
#'
#' @param df Survey data
#' @param data Survey data
#' @param survey Choose between leo2010 and leo2018 (leo2018hoch implements weighting based on population totals).
#' @param replist When true, the function returns an imputation list instead of survey design object.
#' @return A survey design or imputation list object.
#'
#' @importFrom rlang .data
#' @export
leo_svydesign <- function(df, survey = "leo2018", replist = FALSE) {
names(df) <- tolower(names(df))
leo_svydesign <- function(data, survey = "leo2018", replist = FALSE) {
names(data) <- tolower(names(data))
# create survey design object for LEO 2010 or LEO 2018 ----
if (survey == "leo2010" || survey == "leo2018" || survey == "leo2018hoch") {
if (survey == "leo2010") {
# remove logits
df <- df |> dplyr::select(-tidyselect::matches("^pv[1-5]$"))
data <- data |> dplyr::select(-tidyselect::matches("^pv[1-5]$"))
# rename variables for pvs and levels
old=c("pv1_62", "pv2_62", "pv3_62", "pv4_62", "pv5_62",
"pv1_alphalevel_62", "pv2_alphalevel_62", "pv3_alphalevel_62", "pv4_alphalevel_62", "pv5_alphalevel_62")
new=c("pv1", "pv2", "pv3", "pv4", "pv5",
"alpha_pv1", "alpha_pv2", "alpha_pv3", "alpha_pv4", "alpha_pv5")
df <- df |> dplyr::rename_at(dplyr::vars(old), ~new)
data <- data |> dplyr::rename_at(dplyr::vars(old), ~new)
}
# some functions need a helper variable
df$a <- as.numeric(1)
data$a <- as.numeric(1)
# get column names of pvs and levels
lit <- grep("^pv[0-9]+", colnames(df), value = TRUE)
alp <- grep("^alpha_pv[0-9]+", colnames(df), value = TRUE)
lit <- grep("^pv[0-9]+", colnames(data), value = TRUE)
alp <- grep("^alpha_pv[0-9]+", colnames(data), value = TRUE)
# column names of all other variables
all <- df |> dplyr::select(-tidyselect::one_of(lit), -tidyselect::one_of(alp))
all <- data |> dplyr::select(-tidyselect::one_of(lit), -tidyselect::one_of(alp))
all <- colnames(all)
# create list with one pv-variable and one level-variable each (and all other variables)
repList <- lit |>
purrr::map(function(x) {dplyr::select(df, all, paste0("alpha_", x), x) |>
purrr::map(function(x) {dplyr::select(data, all, paste0("alpha_", x), x) |>
dplyr::rename(pv = !!x) |>
dplyr::rename(alpha_pv = !!paste0("alpha_", x))})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment