table display by kable

Codes below display table. I like to see subject and direction columns display once based on subject group (see attached excel sheet). Is that doable in R? Thank you!
tb2smp


title: "Untitled"

output: pdf_document
date: "2023-07-19"

knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(tidyverse)
library(gridExtra)
library(knitr)
library(kableExtra)


disp <- tibble::tribble(
   ~Subject, ~Direction,    ~Peiod,              ~Rate,
  "ALC-05a",    "Lower", "10/2021",                  0,
  "ALC-05a",    "Lower", "11/2021",               0.05,
  "ALC-05a",    "Lower", "12/2021",              0.025,
  "ALC-05a",    "Lower", "10/2021",                  0,
  "ALC-05b",     "High", "11/2021",  0.012987012987013,
  "ALC-05b",     "High", "12/2021", 0.0208333333333333,
  "ALC-05b",     "High", "10/2021",                  0,
  "ALC-05b",     "High", "11/2021",             0.0046,
  "ALC-05b",     "High", "12/2021",                  0
  )

df <- disp %>% as.data.frame() %>% 
  mutate(Rate=Rate*100)

df %>%
  kable(digits=1,"latex", align='lccc') %>%
  kable_styling('bordered', font_size =10, position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="9cm",border_left = TRUE) %>% 
  column_spec(2:3, width="1.8cm") %>%
  column_spec(4,width="2cm",border_right = TRUE) 

Below is one way to remove duplicated values using the lag() function.

df <- disp %>% as.data.frame() %>% 
  mutate(Rate=Rate*100) %>%
  group_by(Subject) %>%
  mutate(Subject = ifelse(row_number() > 1 & Subject == lag(Subject), "", Subject),
         Direction = ifelse(row_number() > 1 & Direction == lag(Direction), "", Direction)) %>%
  ungroup()

1 Like

This topic was automatically closed 7 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.