First, I am looking at this article (https://gt.rstudio.com/reference/tab_row_group.html). It is well written and rather clear but trying to do something that is not explicitly outlined. My first attempt is below the sample data. I can get the first grouping, Nevada, to show up. My question is how I get the other 3 states to show up in the same way?
Here is the sample data and the desired result below. I am looking to group by the area.
library(gt)
library(dplyr)
order2 <- c(1,1,2,2,3,3,4,4)
area2 <- c("Nevada", "Nevada", "California", "California", "Arizona", "Arizona", "Utah", "Utah")
estemp2 <- c(100,200,105,205,300,305,400,405)
projemp2 <- c(110,115,210,215,310,315,410,415)
test <- data.frame(order2,area2,estemp2,projemp2)
testitem <- test %>% gt %>% tab_row_group(label = "Nevada", rows = order <2)
Desired output:
estemp projemp
Nevada 100 110
200 115
California 105 210
205 215
Arizona 300 310
305 315
Utah 400 410
405 415
We could iterate in a for
loop
library(dplyr)
library(gt)
testitem <- test %>%
select(-order2) %>%
gt(rowname_col = "area2")
for(nm in rev(unique(test$area2)))
testitem <- testitem %>%
tab_row_group(label = nm, rows = which(test$area2 == nm))
testitem
If we don't want tab_row_group
, just do a group_by
and convert to gt
test %>%
select(-order2) %>%
group_by(area2) %>%
gt()
-output