0

I am trying to create a function to get frequencies and proportions for a table1 with this code

my.render.cat <- function(x) { c("", sapply(stats.default(x), function(y) with(y, sprintf("%d (%0.0f %%)", FREQ, PCT)))) }

but when I apply it to the function table1() I get the results for proportions with decimal point instead of comma.

I've tried with options(OutDec= ",") but nothing changes Nor by changing Sys.locale for LC NUMERIC.

I've also tried with format(table1, decimal.mark=",") but it doesn't work for table1 output

any suggestions?

I add a reproducible example using iris dataset and formatting Sepal.length to be able to calculate proportions.

table1::table1(~factor(Sepal.Length) | Species, data = iris, render.categorical=my.render.cat)

Ale Rey
  • 75
  • 8
  • try library `scales`, you can use function `number()` – Zulkifli May 26 '21 at 03:06
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 26 '21 at 03:06
  • @MrFlick I've edited the post to create a reproducible example – Ale Rey May 26 '21 at 03:17
  • very sory again. `table1()` function is from "table1" package – Ale Rey May 26 '21 at 03:21

1 Answers1

1

You can use sub to replace . with ,.

library(table1)

my.render.cat <- function(x) { 
  sub('.', ',', c("", sapply(stats.default(x), 
  function(y) with(y, sprintf("%d (%0.2f %%)", FREQ, PCT)))), fixed = TRUE) 
}

table1(~factor(Sepal.Length) | Species, data = iris, 
       render.categorical=my.render.cat)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213