0

How do I limit the number of decimal places in one variable in my dataframe in R to two decimal places? I imported data into R, and the decimal places for just one column went nuts. I want to keep the exact same dataframe but limit the decimal places in that one column to two.

The name of my dataframe is KoreanCorr and the column I want to edit is SA. I understand I do something in the realm of KoreanCorr$SA, digits = 2? What's the full code?

Thanks!

Todd
  • 23
  • 1
  • 1
  • 2

1 Answers1

4

You can use round() option or digit. The following code shows exactly two decimal places for the number using round()

format(round(x, 2), nsmall = 2)

For example:

> format(round(1.20, 2), nsmall = 2) [1] "1.20"
> format(round(1, 2), nsmall = 2) [1] "1.00"
> format(round(1.1234, 2), nsmall = 2) [1] "1.12"

You can format a number, say x, up to decimal places as your wish. Here x is a number with big decimal places , you can format decimal places as your wish. Such that we wish to take up to 8 decimal places of this number.

x<-c(1111111234.6547389758965789345) 
y<-formatC(x,digits=8,format="f")

[1] "1111111234.65473890"
Enrique
  • 842
  • 1
  • 9
  • 21