Search code examples
rhtml-table

How to format table1 values with comma as the thousands separator


I wonder if someone can help me to format a table1. I need to format my "Sum" continuous values with commas. Code and screenshot below: Thanks in advance

```{r}
library(boot) 
library(table1)

melanoma2 <- melanoma
melanoma2$status <- 
  factor(melanoma2$status, 
         levels=c(2,1,3),
         labels=c("Alive", # Reference
                  "Melanoma death", 
                  "Non-melanoma death"))
table1(~ age + thickness | status, render.continuous=c(.="Sum"), data=melanoma2)
```

enter image description here


Solution

  • You can define your own render.continuous function to format the values.

    library(table1)
    library(boot)
    
    table1(~ age + thickness | status, 
           render.continuous = \(x) c("", "Sum" = format(sum(x), big.mark = ",")), 
           data = melanoma2)
    

    table1_render_continuous