1

The output of my code is following:

print(c(ax,bx,cx,dx))
>[1] 232.0000 2.0000 0.6578 1.2356

where in all the cases, the first two numbers are integers, and the rest are real. What should I include in the print statement, such that the output is:

>[1] 232 2 0.6578 1.2356

I know this must be very simple, but somehow I could not figure this out. Thanks for your help :)

  • 1
    You don't specify where you need this, like in markdown or cuarto. But `sprintf` can handle specific printing of values. In your case: `sprintf("%d %d %.4f %.4f", ax, bx, cx, dx)`. – phiver Jul 24 '22 at 09:27

2 Answers2

2

Thanks to @user2554330, for mentioning using noquote like this:

ax = 232.0000 
bx = 2.0000 
cx = 0.6578 
dx = 1.2356
print(noquote(as.character(c(ax,bx,cx,dx))))
#> [1] 232    2      0.6578 1.2356

Created on 2022-07-24 by the reprex package (v2.0.1)

You could use as.character to remove the trailing zeros like this:

ax = 232.0000 
bx = 2.0000 
cx = 0.6578 
dx = 1.2356
print(as.character(c(ax,bx,cx,dx)))
#> [1] "232"    "2"      "0.6578" "1.2356"

Created on 2022-07-24 by the reprex package (v2.0.1)

Quinten
  • 35,235
  • 5
  • 20
  • 53
1

While I like noquote, replacing all trailing zeros will have the same effect:

sub("0+$", "", as.character(c(ax,bx,cx,dx)))
[1] "232"    "2"      "0.6578" "1.2356"
TarJae
  • 72,363
  • 6
  • 19
  • 66