Home > Mobile >  Whitespaces appear when combining paste0 and format in R
Whitespaces appear when combining paste0 and format in R

Time:12-07

To display the results of a regression I ran, I've got a tibble with estimates and corresponding confidence intervals:

library(tidyverse)
library(magrittr

mydata <- structure(list(term = structure(c(1L, 3L, 4L), .Label = c("Intercept", 
"Follow-up time (years)", "Age (years)", "Sex (male)", "Never smoker (reference)", 
"Current smoker", "Former smoker", "Obesity (=30 kg/m²)", "BMI (kg/m²)", 
"Diabetes", "Glucose (mmol/L)", "Glucose lowering medication use", 
"Hypertension", "Systolic blood pressure (mmHg)", "Diastolic blood pressure (mmHg)", 
"Antihypertensive medication use", "Hypercholesterolemia", "LDL cholesterol (mmol/L)", 
"Lipid lowering medication use", "Chronic kidney disease (mL/min/1.73m²)", 
"=90 (reference)", "60-89", "=60"), class = c("ordered", "factor"
)), estimate = c(518.38, 0.98, 1.07), conf_low = c(178.74, 0.93, 
0.96), conf_high = c(1503.36, 1.03, 1.19), label = c("518.38 (178.74-1503.36)", 
"  0.98 (  0.93-   1.03)", "  1.07 (  0.96-   1.19)")), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

mydata

# A tibble: 3 x 4
  term        estimate conf_low conf_high
  <ord>          <dbl>    <dbl>     <dbl>
1 Intercept     518.     179.     1503.  
2 Age (years)     0.98     0.93      1.03
3 Sex (male)      1.07     0.96      1.19

To make a label that includes the estimate and 95%CI, I've used paste0, and to make sure that every number has two decimals I've used format. However, when combining these, extra whitespaces appear:

mydata <- 
  mydata %>% 
  mutate(
    label=
      paste0(format(round(estimate, digits=2), nsmall=2), 
             " (", 
             format(round(conf_low, digits=2), nsmall=2), 
             "-", 
             format(round(conf_high, digits=2), nsmall=2), 
             ")", 
             sep="", collaps=""))

mydata
# A tibble: 3 x 5
  term        estimate conf_low conf_high label                    
  <ord>          <dbl>    <dbl>     <dbl> <chr>                    
1 Intercept     518.     179.     1503.   "518.38 (178.74-1503.36)"
2 Age (years)     0.98     0.93      1.03 "  0.98 (  0.93-   1.03)"
3 Sex (male)      1.07     0.96      1.19 "  1.07 (  0.96-   1.19)"

Why does this happen? Can I prevent this or otherwise remove the whitespaces so that the format becomes "estimate (conf_low-conf_high)"?

CodePudding user response:

Add trim=TRUE in the format() call:

mydata %>% 
mutate(
  label=
    paste0(format(round(estimate, digits=2), nsmall=2, trim=TRUE), 
           " (", 
           format(round(conf_low, digits=2), nsmall=2, trim=TRUE), 
           "-", 
           format(round(conf_high, digits=2), nsmall=2, trim=TRUE), 
           ")", 
           sep="", collaps=""))
  
# A tibble: 3 × 5
  term        estimate conf_low conf_high label                    
  <ord>          <dbl>    <dbl>     <dbl> <chr>                    
1 Intercept     518.     179.     1503.   "518.38 (178.74-1503.36)"
2 Age (years)     0.98     0.93      1.03 "0.98 (0.93-1.03)"     
3 Sex (male)      1.07     0.96      1.19 "1.07 (0.96-1.19)"     

CodePudding user response:

1) Use sprintf

mydata %>% 
  mutate(label = sprintf("%.2f (%.2f-%.2f)", estimate, conf_low, conf_high))

giving:

# A tibble: 3 x 5
  term        estimate conf_low conf_high label                  
  <ord>          <dbl>    <dbl>     <dbl> <chr>                  
1 Intercept     518.     179.     1503.   518.38 (178.74-1503.36)
2 Age (years)     0.98     0.93      1.03 0.98 (0.93-1.03)       
3 Sex (male)      1.07     0.96      1.19 1.07 (0.96-1.19)       

2) or this variation producing slightly different output

mydata %>% 
  mutate(label = sprintf("%6.2f (%6.2f-%7.2f)", estimate, conf_low, conf_high))

giving;

# A tibble: 3 x 5
  term        estimate conf_low conf_high label                    
  <ord>          <dbl>    <dbl>     <dbl> <chr>                    
1 Intercept     518.     179.     1503.   "518.38 (178.74-1503.36)"
2 Age (years)     0.98     0.93      1.03 "  0.98 (  0.93-   1.03)"
3 Sex (male)      1.07     0.96      1.19 "  1.07 (  0.96-   1.19)"
  • Related