Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round all the values of a prop.table in R in one line?

Tags:

rounding

r

In R 3.4.0, how can I round all the percentages generated by prop.table() to 3 decimal places in one line?

This does not work:

MyTable$MyCol %>% table() %>% prop.table()*100 %>% round(.,3)

        -7          -2          0          1          2          3         4         5 
 0.4672897  0.2336449 31.7757009  1.6355140 44.3925234 20.3271028  0.4672897  0.7009346 

expected something like:

   -7     -2    0       1      2     3       4      5 
 0.467  0.233 31.775 1.635 44.392 20.327  0.467  0.700 
like image 960
Javide Avatar asked Jul 29 '17 06:07

Javide


People also ask

How do you round off values in a column in R?

Round function in R, rounds off the values in its first argument to the specified number of decimal places. Round() function in R rounds off the list of values in vector and also rounds off the column of a dataframe. It can also accomplished using signif() function.

What does Prop table () do in R?

The prop. table() function in R can be used to calculate the value of each cell in a table as a proportion of all values.


1 Answers

You need to separate multiplication into a separate operation:

mtcars$cyl %>% table() %>% prop.table() %>% `*`(100) %>% round(2)
like image 65
dmi3kno Avatar answered Nov 08 '22 12:11

dmi3kno