Display five decimal places and make them right-aligned

> library(tidyverse)
> df <- tibble(v1 = c(10,10.2,9.23,5))
> df
# A tibble: 4 x 1
     v1
  <dbl>
1 10   
2 10.2 
3  9.23
4  5 

How do I show five decimal places? I also want the variable to be right-aligned.

# A tibble: 4 x 1
  wanted    
  <chr>     
1 "10.00000"
2 "10.20000"
3 " 9.23000"
4 " 5.00000"
take_five <- function(x) format(formatC(x,5,format = "f", flag= " "), justify = "right")
(DF <- dplyr::tibble(v1 = c(10,10.2,9.23,5)))
#> # A tibble: 4 × 1
#>      v1
#>   <dbl>
#> 1 10   
#> 2 10.2 
#> 3  9.23
#> 4  5
DF$v1 <- take_five(DF$v1)
DF
#> # A tibble: 4 × 1
#>   v1         
#>   <chr>      
#> 1 " 10.00000"
#> 2 " 10.20000"
#> 3 "  9.23000"
#> 4 "  5.00000"
1 Like

@technocrat Many thanks!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.