Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a integer vector as decimal number

Tags:

r

If you declare a numeric vector with a mix of decimal and integer values and print it, all of them are converted to a decimal number.

s <- c(0, 1.1, 2, 3)
print(s)
[1] 0.0 1.1 2.0 3.0

but if you use cat instead of print, each number is formatted according to its type.

cat(s, sep=" ")
0 1.1 2 3 

How can you print a numeric vector using cat but converting the values to decimal numbers as print does?

Best

like image 837
corto Avatar asked Aug 10 '26 14:08

corto


1 Answers

for example:

> cat(sprintf("%.01f", s))
0.0 1.1 2.0 3.0
like image 165
kohske Avatar answered Aug 13 '26 10:08

kohske



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!