0

I have a dataset that looks something like this. and I am using a modelsummary function to summarise some of its characteristics.

set.seed(1)
df<- data.frame(var1=runif(1000),
                  var2=runif(1000),
                  var3=runif(1000),
                  tr=rbinom(100, size=1, p=0.1)) %>% 
  arrange(desc(tr))
datasummary_balance(~tr,
                    fmt=3,
                    data=df,
                    output = "markdown")

enter image description here

I would like to rename var1 and display "This is my first variable", and also rename the headers, replacing "1" with "Treatment" and "0" with "control"

Does anyone knows how I can do this?

Maybe it is not relevant, but please consider that my final output should be in Latex

thanks

Alex
  • 1,207
  • 9
  • 25

1 Answers1

0

With the datasummary_balance function, you have to rename the variables and use backticks:

library(modelsummary)
library(tidyverse)

dat <- mtcars |>
    mutate(am  = ifelse(am == 0, "Zero", "One")) |>
    select(`Mile per gallon` = mpg,
           `Horsepower` = hp,
           am)

datasummary_balance(~am, data = dat)

enter image description here

In the datasummary function you have a lot more options. See the vignette:

https://vincentarelbundock.github.io/modelsummary/articles/datasummary.html#renaming-with

Vincent
  • 15,809
  • 7
  • 37
  • 39