0

I have two values produced from a linear regression

slope = 2.005455

er_slope = 0.01588526

I would like to round the slope value according to the number of decimal places in er_slope. I know the round function allows for this

round(slope, 2)
2.01

However, I don't want to input the value 2. I wish to use a function which does this automatically based on any er_slope decimal places (in case the value changes in the future).

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    Does this answer your question? [how to return number of decimal places in R](https://stackoverflow.com/questions/5173692/how-to-return-number-of-decimal-places-in-r) – marcguery Feb 27 '21 at 02:51

1 Answers1

0

You can remove everything until "." and count number of characters which can be used in round for er_slope.

slope = 2.005455
er_slope = 0.01588526

nd <- nchar(sub('.*\\.', '', slope))
round(er_slope, nd)
#[1] 0.015885
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213