Search code examples
rstargazer

How to deal with long variable names when using stargazer to make tables in R?


I try to display the first 20 rows of a data frame by using stargazer. But some of the variable names are so long (such as Prevelance of unnourishment (% of population)) that the table just cannot fit in. I understand that renaming the variables with shorter names will work but that's not the way I'm looking for. I also thought about changing the latex codes that has been produced but turned out those cannot be changed. I guess the best way is to do something with the R command. Mine is:

stargazer(as.matrix(data[1:20,]), type='latex')

How should I change it to make the table fit in? Thanks a lot!


Solution

  • use abbreviate to shorten names. You can control the length of names by adjusting minlength argument. For more info, please read ?abbreviate

    By following this, sometimes non-unique names may appear, so to take care of it, you would use make.unique on the abbreviated names.

    colnames(data) <- abbreviate( colnames(data), minlength = 3, strict = TRUE )
    stargazer(as.matrix(data[1:20,]), type='latex')