Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to round the results of the prop.table function in R?

I would like to round the results of the prop.table to one decimal place but it does not seem it work as intended. Any leads?

with(mtcars, table(vs, am)) |> prop.table(margin = 1) * 100 |> round()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Its all about the paranthesis:

with(mtcars, table(vs, am) |> prop.table(margin = 1) * 100) |>round(1)

   am
vs     0    1
  0 66.7 33.3
  1 50.0 50.0

Note that what you have does not round the results of prop.table but rather rounds 100. If you want you could do

(with(mtcars, table(vs, am)) |> prop.table(margin = 1) * 100)|>round(1)

  am
vs     0    1
  0 66.7 33.3
  1 50.0 50.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading