Multi-level heading with nice_table?

I have perused several sites and watched a couple of videos on the package rempsyc ( an all of its affiliated packages) but I am having trouble incorporating the multilevel spanner. Has anyone used this before, or has some suggestions? I feel like it should be quite simple but i keep getting errors. I have the following packages installed:
library(dplyr)
library(forcats)
library(tidyverse)
library(rempsyc)
library(knitr)
library(flextable)

pr_counts<- table(as.character(court_data$ProsRace_coll))

# Calculate percentages
total_count <- sum(pr_counts)
pr_percentage_data <- data.frame(
  Character_Type = names(pr_counts),
  Count = as.numeric(pr_counts),
  Percentage = (pr_counts / total_count) * 100
  ) %>%
  select(Character_Type, Percentage.Freq)

total_count <- sum(pr_counts)
pr_percentage_data <- data.frame(
  Race = c("Black/African American", "White", "Other"),
  N = c(pr_counts[1], pr_counts[2], pr_counts[3]),
  Percentage = c(pr_counts[1], pr_counts[2], pr_counts[3]) / total_count * 100
)

juv_counts <- table(as.character(court_data$JuvRace_coll))
total_juv_count <- sum(juv_counts)
juv_percentage_data <- data.frame(
N = c(juv_counts[1], juv_counts[2],juv_counts[3]),
Percentage = c(juv_counts[1], juv_counts[2], juv_counts[3]) / total_count * 100
)


def_counts <- table(as.character(court_data$DefRace_coll))
total_def_count <- sum(def_counts)
def_percentage_data <- data.frame(
  N = c(def_counts[1], def_counts[2],def_counts[3]),
  Percentage = c(def_counts[1], def_counts[2], def_counts[3]) / total_count * 100
)

combined_percentage_data <- data.frame(
  Race = pr_percentage_data$Race,
  Youth = juv_percentage_data$Percentage,
  Defender = def_percentage_data$Percentage,
  Prosecutor = pr_percentage_data$Percentage
  )

testtable<- nice_table(combined_percentage_data)
testtable <- testtable %>%
  split_header(col_keys = c("Youth", "Defender", "Prosecutor"), labels = "Hearing Level") %>%
  merge_header(col_keys = "Race", label = "Personal Characteristics") %>%
  autofit()

The last 5 lines of this code generates errors. This image shows how I want the table to be structured (sans judge level). I know there are probable several ways to do this and I just need one. Please help if you can. Much appreciated !

1 Like

Thanks for providing code. Could you kindly take further steps to make it easier for other forum users to help you? Share some representative data that will enable your code to run and show the problematic behaviour.

How do I share data for a reprex?

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

This is the structure as detailed by reprex

head(combined_percentage_data)
combined_percentage_data<- data.frame(
  stringsAsFactors = FALSE,
              Race = c("Black/African American", "White", "Other"),
             Youth = c(72, 12, 15.5),
          Defender = c(38.5, 55.5, 6),
        Prosecutor = c(43, 42.5, 14.5)
)

head(combined_percentage_data)

   Race Youth Defender Prosecutor
1 Black/African American  72.0     38.5       43.0
2                  White  12.0     55.5       42.5
3                  Other  15.5      6.0       14.5

This is how its comes out as testtable ( see image)

1 Like
library(rempsyc)
library(flextable)

combined_percentage_data<- data.frame(
  stringsAsFactors = FALSE,
  Race = c("Black/African American", "White", "Other"),
  Youth = c(72, 12, 15.5),
  Defender = c(38.5, 55.5, 6),
  Prosecutor = c(43, 42.5, 14.5)
)


testtable<- nice_table(combined_percentage_data)
testtable <- testtable |>
  add_header_row(top = TRUE, values = c("Personal Characteristics",
                                        "Hearing Level"),colwidths = c(1,3))
testtable

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.