- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 each value to two decimal places in columns if some columns are categorical in R data frame?
To round each value to two decimal places in columns if some columns are categorical in R data frame, we can follow the below steps −
First of all, create a data frame.
Then, use numcolwise function from plyr package to round each value to two decimal places in columns if some columns are categorical.
Example
Create the data frame
Let’s create a data frame as shown below −
Level<-sample(c("low","medium","high"),25,replace=TRUE) Group<-sample(c("first","second"),25,replace=TRUE) DV1<-rnorm(25) DV2<-rnorm(25) df<-data.frame(Level,Group,DV1,DV2) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Level Group DV1 DV2 1 high second 0.44421128 -1.64570047 2 high first 1.43670027 -0.88397636 3 low second -1.18341254 1.60293111 4 high first -0.62283226 -0.19380057 5 medium first 1.00604412 0.25331793 6 low second 1.40551557 -0.18606767 7 high second -2.35957053 0.67014007 8 medium first 0.80687847 -1.45673170 9 high first -0.54953362 0.56315219 10 medium second -0.04559434 0.21357310 11 medium second -0.31043620 -0.53737110 12 low first 1.01480003 -0.21291348 13 low second 0.93591125 0.31726701 14 medium second 0.15091510 -0.44046938 15 low first -0.12028441 -1.04923163 16 medium second 3.26870341 -0.12206452 17 low first 0.43235968 -0.30970690 18 high first 0.30046505 -0.50644433 19 high first -1.93352142 -1.06623806 20 medium first 1.00902072 0.09869907 21 medium second -0.75597488 0.03825807 22 high second -0.45890036 -1.58441677 23 high second -4.05130949 -0.51986084 24 medium second -0.15283662 0.30010858 25 high second -0.97502595 0.01697664
Round to two decimal places each value in columns if some columns are categorical
Using numcolwise function from plyr package to round each value in numerical columns to two decimal places if some columns are categorical in the data frame df −
Level<-sample(c("low","medium","high"),25,replace=TRUE) Group<-sample(c("first","second"),25,replace=TRUE) DV1<-rnorm(25) DV2<-rnorm(25) df<-data.frame(Level,Group,DV1,DV2) library(plyr) numcolwise(round,2)(df)
Output
DV1 DV2 1 0.44 -1.65 2 1.44 -0.88 3 -1.18 1.60 4 -0.62 -0.19 5 1.01 0.25 6 1.41 -0.19 7 -2.36 0.67 8 0.81 -1.46 9 -0.55 0.56 10 -0.05 0.21 11 -0.31 -0.54 12 1.01 -0.21 13 0.94 0.32 14 0.15 -0.44 15 -0.12 -1.05 16 3.27 -0.12 17 0.43 -0.31 18 0.30 -0.51 19 -1.93 -1.07 20 1.01 0.10 21 -0.76 0.04 22 -0.46 -1.58 23 -4.05 -0.52 24 -0.15 0.30 25 -0.98 0.02
Advertisements