Reputation: 6755
In r by default the print method seems to pad printed numbers with extra 0s to the right of the decimal place so that all of the numbers are displayed with the same number of decimals, which is equal to the maximum number of decimals.
For example
a <- c(5, 3.25, 7.9999)
b <- c(5.1, 3.2, 8.3)
data.frame(a, b)
yields
a b
1 5.0000 5.1
2 3.2500 3.2
3 7.9999 8.3
I know I can use options
to change the maximum number of digits but that will just change the number of 0s added. I don't want to change them to characters because I'd like to be able to work with the numbers afterwards. I just want to print the values as they are actually stored in the data frame (or other) object.
Is there any way to do that?
Note: This is not the same as asking how to set a fixed number of digits either generally or dynamically. The proposed duplicate questions address that issue.
Upvotes: 1
Views: 57
Reputation: 887158
Instead of creating a vector
, we can store it in list
and that will preserve the digits as in the original
list(5, 3.25, 7.9999)
#[[1]]
#[1] 5
#[[2]]
#[1] 3.25
#[[3]]
#[1] 7.9999
If we really need a data.frame
data.frame(a = I(list(5, 3.25, 7.9999)), b = I(list(5.1, 3.2, 8.3)))
# a b
#1 5 5.1
#2 3.25 3.2
#3 7.9999 8.3
Upvotes: 1