

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to round values in proportion table in R?
To round values in proportion table in R, we can first save the proportion table in an object and then use the round function.
For example, if we have a vector say X then we can create a proportion table for data in X using prop.table(table(X)) and store it in an object called Tab and then round the values to two decimal places by using the below given command −
round(Tab,2)
Example 1
Following snippet creates a sample data frame −
x1<-rpois(200,5) x1
Output
The following dataframe is created −
[1] 7 9 1 6 1 5 5 10 5 6 2 3 3 7 6 5 7 0 4 4 6 2 4 5 7 [26] 4 2 7 3 3 5 9 5 6 3 6 3 5 4 6 3 6 3 6 7 3 6 4 5 4 [51] 2 4 2 6 4 4 6 9 6 5 9 3 2 7 4 7 2 4 5 9 7 2 4 6 9 [76] 7 4 6 2 4 5 5 7 8 5 9 6 5 8 9 2 6 7 2 3 3 4 5 3 2 [101] 9 7 3 8 7 4 7 2 7 6 5 4 2 3 2 6 7 1 3 4 3 7 7 3 7 [126] 6 3 2 10 4 10 3 3 3 10 6 6 3 6 3 5 6 3 4 4 1 2 6 5 7 [151] 4 5 1 8 2 6 1 5 4 4 5 7 5 3 2 3 7 6 7 6 8 6 2 7 4 [176] 3 3 5 4 7 4 5 10 8 2 4 6 6 7 4 7 2 6 9 4 3 6 5 4 3
Add the following code to the above snippet −
x1<-rpois(200,5) table1<-prop.table(table(x1)) table1
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 0 1 2 3 4 5 6 7 8 9 10 0.005 0.030 0.110 0.155 0.160 0.130 0.165 0.140 0.030 0.050 0.025
Add the following code to the above snippet −
x1<-rpois(200,5) table1<-prop.table(table(x1)) round(table1,2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 0 1 2 3 4 5 6 7 8 9 10 0.00 0.03 0.11 0.16 0.16 0.13 0.16 0.14 0.03 0.05 0.03
Example 2
Following snippet creates a sample data frame −
x2<-rpois(200,1) x2
The following dataframe is created −
[1] 0 0 0 2 0 1 1 0 1 1 1 3 0 2 0 0 0 2 1 0 2 0 2 2 1 1 1 2 2 0 3 0 1 0 1 1 1 [38] 1 1 2 2 0 3 0 1 0 1 0 0 3 1 0 2 0 1 0 3 4 1 2 0 2 0 0 1 1 1 0 0 0 2 1 0 2 [75] 1 1 3 1 1 0 1 0 2 1 0 0 4 1 3 0 0 0 1 2 0 1 1 2 0 0 2 0 1 0 1 1 2 3 0 1 1 [112] 1 1 0 4 2 0 0 1 0 2 1 1 4 2 3 0 0 0 0 0 1 0 1 1 1 1 1 0 1 4 1 2 1 0 0 0 0 [149] 2 1 0 1 1 1 2 2 3 3 2 0 3 0 2 0 0 0 1 0 3 0 1 1 3 2 3 0 2 1 1 1 1 0 0 0 0 [186] 0 2 0 3 3 0 1 0 0 0 3 3 0 0 1
Add the following code to the above snippet −
x2<-rpois(200,1) table2<-prop.table(table(x2)) table2
Output
If you execute all the above given snippets as a single program, it generates the following output −
x2 0 1 2 3 4 0.390 0.330 0.160 0.095 0.025
Add the following code to the above snippet −
x2<-rpois(200,1) table2<-prop.table(table(x2)) round(table2,2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x2 0 1 2 3 4 0.39 0.33 0.16 0.10 0.03
Example 3
Following snippet creates a sample data frame −
x3<-rpois(200,10) x3
The following dataframe is created −
[1] 8 6 8 5 8 5 9 9 13 13 15 12 13 9 10 9 7 8 8 9 15 9 11 10 4 [26] 12 9 13 14 7 9 7 9 5 11 9 12 13 10 10 4 11 9 16 12 8 8 10 9 10 [51] 14 11 17 10 11 13 5 13 16 16 11 14 16 9 3 8 8 5 4 10 8 9 12 9 8 [76] 8 14 3 10 15 8 18 11 8 10 5 5 7 10 10 11 8 10 10 16 8 11 12 13 11 [101] 13 13 6 14 9 6 9 5 8 12 12 10 10 12 5 19 7 6 9 6 5 13 10 11 9 [126] 8 7 14 12 8 11 14 14 9 7 13 5 9 11 8 12 8 16 13 11 7 17 10 12 15 [151] 11 13 10 18 13 5 8 8 16 8 9 9 8 14 4 13 9 7 14 6 10 10 12 14 10 [176] 7 13 9 4 9 12 10 14 3 14 10 8 11 7 12 10 12 9 5 6 9 10 9 12 5
Add the following code to the above snippet −
x3<-rpois(200,10) table3<-prop.table(table(x3)) table3
Output
If you execute all the above given snippets as a single program, it generates the following output −
x3 3 4 5 6 7 8 9 10 11 12 13 14 15 0.015 0.025 0.070 0.035 0.055 0.130 0.145 0.130 0.080 0.085 0.085 0.065 0.020 16 17 18 19 0.035 0.010 0.010 0.005
Add the following code to the above snippet −
x3<-rpois(200,10) table3<-prop.table(table(x3)) round(table3,2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x3 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0.01 0.03 0.07 0.04 0.06 0.13 0.14 0.13 0.08 0.09 0.09 0.06 0.02 0.04 0.01 0.01 19 0.00
Example 4
Following snippet creates a sample data frame −
x4<-sample(0:2,200,replace=TRUE) x4
The following dataframe is created −
[1] 0 0 2 0 0 1 0 2 2 2 0 2 2 2 0 2 0 0 2 0 2 0 1 2 1 2 0 2 1 1 0 1 2 2 1 1 1 [38] 2 1 2 2 1 0 2 2 0 2 0 1 1 1 0 2 2 1 1 0 0 2 0 2 1 0 2 1 2 1 1 1 2 0 2 2 0 [75] 1 1 1 1 1 0 2 0 0 2 0 2 2 1 0 0 2 1 1 2 0 2 1 0 2 2 1 1 1 0 2 2 1 2 0 0 1 [112] 2 2 1 1 1 1 2 0 1 1 1 0 0 2 2 1 1 1 2 0 0 0 1 2 0 1 0 1 2 0 2 2 1 1 2 0 1 [149] 2 1 1 1 1 0 2 1 2 0 1 2 0 0 2 0 1 2 2 0 2 2 0 0 0 2 1 2 1 0 0 1 1 2 0 2 1 [186] 0 1 0 2 0 1 2 0 0 1 1 2 2 2 0
Add the following code to the above snippet −
x4<-sample(0:2,200,replace=TRUE) table4<-prop.table(table(x4)) table4
Output
If you execute all the above given snippets as a single program, it generates the following output −
x4 0 1 2 0.310 0.335 0.355
Add the following code to the above snippet −
x4<-sample(0:2,200,replace=TRUE) table4<-prop.table(table(x4)) round(table4,2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x4 0 1 2 0.31 0.34 0.36
Example 5
Following snippet creates a sample data frame −
x5<-sample(rpois(3,5),200,replace=TRUE) x5
The following dataframe is created −
[1] 4 7 4 4 7 3 4 7 4 4 4 3 7 3 4 7 3 3 7 4 7 7 3 3 4 7 7 4 7 4 3 3 4 3 3 3 4 [38] 7 3 3 7 4 4 4 3 7 4 3 3 7 7 3 7 4 4 7 7 3 4 4 4 4 3 3 3 4 3 3 4 4 7 7 4 4 [75] 3 7 4 4 7 4 7 3 4 3 7 3 3 4 7 3 4 3 3 3 7 4 7 3 4 4 7 7 3 4 7 3 3 7 3 7 7 [112] 7 4 4 7 7 4 3 7 3 3 3 7 3 4 4 3 3 4 7 3 4 7 4 4 4 3 3 7 3 4 3 7 4 4 3 7 3 [149] 4 3 7 4 3 4 7 4 4 4 4 3 3 4 3 3 4 3 4 7 7 7 4 3 4 4 3 7 7 4 4 3 4 3 3 3 7 [186] 7 7 3 4 3 7 3 4 3 7 4 4 7 3 4
Add the following code to the above snippet −
x5<-sample(rpois(3,5),200,replace=TRUE) table5<-prop.table(table(x5)) table5
Output
If you execute all the above given snippets as a single program, it generates the following output −
x5 3 4 7 0.350 0.365 0.285
Add the following code to the above snippet −
x5<-sample(rpois(3,5),200,replace=TRUE) table5<-prop.table(table(x5)) round(table5,2)
Output
If you execute all the above given snippets as a single program, it generates the following output −
x5 3 4 7 0.35 0.36 0.28
- Related Questions & Answers
- How to round matrix values in R?
- How to add proportion total at margins on a table in R?
- How to find the proportion of row values in an R data frame?
- How to find the proportion using normal distribution in R?
- How to access the table values in R?
- How to round exponential numbers in R?
- How to round correlation values in the correlation matrix to zero decimal places in R?
- How to round the summary output in R?
- How to round to the nearest hundred in R?
- How to find the sample size for two sample proportion tests with given power in R?
- Who represented congress in the second round table conference?
- Floor the decimal values in MySQL instead of round?
- How to create a frequency table of a vector that contains repeated values in R?
- How to find the proportion of categories based on another categorical column in R's data.table object?
- How to show values in boxplot in R?