kodebok_funksjoner: AblaNor kodebok functions

kodebok_funksjonerR Documentation

AblaNor kodebok functions

Description

Functions for adding and removing listeverdi- and listetekst- variables in AblaNor-data. Functions are:

  • kodebok_sjekk_foer_leggtil() checks compatibility between df and kb for a given variable verdi_variabel and says whether corresponding tekst_variabel can be added in df.

  • kodebok_sjekk_foer_fjerning() checks compatibility between df and kb for a given variable tekst_variabel and says and whether corresponding verdi_variabel can be removed from df.

  • kodebok_fyll_listetekstvar() adds some/all listetekst-variables in df.

  • kodebok_beholde_bare_listetekstvar() removes some/all listeverdi-variables from df.

Usage

kodebok_sjekk_foer_leggtil(df, verdi_variabel, tekst_variabel, koder)

kodebok_sjekk_foer_fjerning(
  df,
  kb,
  verdi_variabel,
  tekst_variabel,
  type = "Listevariabel"
)

kodebok_fyll_avkrysningsboks(df, kb, ..., suffiks = "_tekst")

kodebok_fyll_listetekstvar(df, kb, ..., suffiks = "_tekst")

kodebok_beholde_bare_listetekstvar(
  df,
  kb,
  ...,
  suffiks = "_tekst",
  fjerne_suffiks_fra_navn = TRUE
)

Arguments

df

data.frame containing variables from AblaNor e.g. d_ablanor.

verdi_variabel

is used to check compatibility between df and kb for one variable. verdi_variabel is the name of variable with numerical values (listeverdier).

tekst_variabel

is used to check compatibility between df and kb for one variable. tekst_variabel is the name of variable with text values (listetekst).

koder

is used to check compatibility between df and kb for one variable. koder contains selected rows from kb.

kb

data.frame with kodeboken. Must contain variables type, fysisk_feltnavn, listeverdier and listetekst. One row for each level of all variables.

type

String. Contains the type of variable as defined in kb Listevariabel or Avkrysninsboks

...

If only selected listetekst- or listeverdi- variables are to be added or removed in df, the selected variables should be listed here, separated by commas.

suffiks

string value that contains suffix of new listetekst- variables to be added to or listeverdi-variables to be removed from df. Default value is "_tekst".

fjerne_suffiks_fra_navn

boolean. If TRUE the listetekst- variables are renamed and suffix is removed from variable names. If FALSE, variable-names are not changed and will contain suffix.

Examples

 df <- data.frame(var1 = c(1:5, 1:5),
                  var2 = rep(c(0,1), 5))
 kb <- data.frame(fysisk_feltnavn = c(rep("var1", 5), "var2", "var2"),
                  type = rep("Listevariabel", 7),
                  listeverdier = c(1:5, 0, 1),
                  listetekst = c(letters[1:5], "nei", "ja"))

 ablanor::kodebok_sjekk_foer_leggtil(
                         df = df,
                         tekst_variabel = "var2_tekst",
                         verdi_variabel = "var2",
                         koder = kb %>%
                           dplyr::filter(.data$fysisk_feltnavn == "var2"))

ablanor::kodebok_fyll_listetekstvar(df = df,
                                    kb = kb,
                                    suffiks = "_tekst")
df <- data.frame(var1 = c(1:5),
                 var1_tekst = letters[1:5],
                 var3 = c(0, 0, 1, 1, 1),
                 var3_tekst = c("nei", "nei", "ja", "ja", "ja"))

kb <- data.frame(fysisk_feltnavn = c(rep("var1", 5), "var3", "var3"),
                 type = rep("Listevariabel", 7),
                 listeverdier = c(1:5, 0, 1),
                 listetekst = c(letters[1:5],"nei", "ja"))
ablanor::kodebok_sjekk_foer_fjerning(df = df,
                                     kb,
                                     verdi_variabel = "var3",
                                     tekst_variabel = "var3_tekst",
                                     type = "Listevariabel")
ablanor::kodebok_beholde_bare_listetekstvar(
                           df = df,
                           kb = kb,
                           suffiks = "_tekst",
                           fjerne_suffiks_fra_navn = TRUE)

Rapporteket/ablanor documentation built on Feb. 27, 2025, 8:26 p.m.