Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does as.character drop decimal point?

Tags:

r

I'm interested to know why as.character(5.0) returns 5 but as.character(5.1) returns 5.1 in R. I tried to get an answer by reading the documentation but had no luck.

like image 364
user260474 Avatar asked Oct 21 '19 19:10

user260474


1 Answers

I'm interested to know why as.character(5.0) returns 5

The key word here is "returns." What do you mean by that? Note that typing this in the console gives you 5:

> 5.0
[1] 5

5 is the same things as 5.0 for the purposes of calculation. So what you probably really care about is how 5 is printed. You thus need to use joran's method or a function like sprintf.

like image 178
eable Avatar answered Nov 19 '22 10:11

eable