R/add_n_vars_to_chapter_structure.R

Defines functions add_n_vars_to_chapter_structure

add_n_vars_to_chapter_structure <-
  function(chapter_structure,
           target_variable = ".variable_name_dep",
           variable_name = ".n_dep") {
    grouping_structure <- dplyr::group_vars(chapter_structure)

    chapter_structure |>
      dplyr::group_map(
        .keep = TRUE,
        .f = ~ {
          uniques <- as.character(unique(.x[[target_variable]]))
          if (!is.na(length(uniques[!is.na(uniques)]))) {
            .x[[variable_name]] <-
              length(uniques[!is.na(uniques)])
          } else {
            .x[[variable_name]] <- 0
          }
          .x
        }
      ) |>
      dplyr::bind_rows() |>
      dplyr::grouped_df(grouping_structure)
  }

Try the saros.base package in your browser

Any scripts or data that you put into this service are public.

saros.base documentation built on June 8, 2025, 10:03 a.m.