r keeping 0.0 when using paste or paste0

后端 未结 3 523
野的像风
野的像风 2021-01-19 01:41

This is a simple question but it is starting to annoy me that I cant find a solution....

I would like to be able to keep the 0.0 when using it as an output when usin

3条回答
  •  滥情空心
    2021-01-19 02:11

    There is also format and drop0trailing

    paste0('x',format(y,drop0Trailing = F),'x')
    

    And, if you really want only 0 replaced with 0.0, not (1.0 or -1.0) then

    paste0('x',gsub(x = gsub(x = format(y, drop0trailing = T),'0$', '0.0'),' ',''),'x')
    ## [1] "x-1.5x" "x-1x"   "x-0.5x" "x0.0x"  "x0.5x"  "x1x"    "x1.5x" 
    

    Or, as @mrdwab suggested (and is less typying)

    paste0('x',gsub("^0$", "0.0", as.character(y)),'x')
    

提交回复
热议问题