user2537291
user2537291

Reputation: 81

Converting a string to a number in R

I'm having trouble with simply writing a function that turns a string representation of a number into a decimal representation. To boil the issue down to essentials, consider the following function:

   f <- function(x) {
      y <- as.numeric(x)
      return(y)
   }

When I apply this function to the string "47.418" I get back 47.42, but what I want to get back is 47.418. It seems like the return value is being rounded for some reason.

Any suggestions would be appreciated

Upvotes: 1

Views: 494

Answers (1)

IRTFM
IRTFM

Reputation: 263481

You have done something to your print options. I get no rounding:

> f <- function(x) { y <- as.numeric(x); return(y) }
> f(47.418)
[1] 47.418

?options

The default value for digits is 7:

> options("digits")
$digits
[1] 7

Further questions should be accompanied by dput() on the object in question.

Upvotes: 3

Related Questions