Create Sub Headers in R

Solution 1:

library(tidyverse)

input %>%
  pivot_longer(-Region) %>%
  na.omit() %>%
  group_by(Region, name) %>%
  mutate(id = row_number()) %>%
  pivot_wider(names_from = id, values_from = value) %>%
  data.table::transpose(make.names = TRUE)
  
  Central Central Central Pacific Pacific Pacific Eastern Eastern Eastern
1   Day_1   Day_2   Day_3   Day_2   Day_1   Day_3   Day_1   Day_2   Day_3
2    Boat    Shoe   Table    Boat   Table    Shoe    Boat    Shoe   Table
3    <NA>    <NA>    <NA>    <NA>    <NA>    <NA>   Watch     Cup    <NA>
4    <NA>    <NA>    <NA>    <NA>    <NA>    <NA>   Chair    <NA>    <NA>