在R中组合paste0和format时出现空格

tcv*_*992 0 string format r dplyr

为了显示我运行的回归结果,我有一个包含估计值和相应置信区间的小标题:

\n
library(tidyverse)\nlibrary(magrittr\n\nmydata <- structure(list(term = structure(c(1L, 3L, 4L), .Label = c("Intercept", \n"Follow-up time (years)", "Age (years)", "Sex (male)", "Never smoker (reference)", \n"Current smoker", "Former smoker", "Obesity (=30 kg/m\xc2\xb2)", "BMI (kg/m\xc2\xb2)", \n"Diabetes", "Glucose (mmol/L)", "Glucose lowering medication use", \n"Hypertension", "Systolic blood pressure (mmHg)", "Diastolic blood pressure (mmHg)", \n"Antihypertensive medication use", "Hypercholesterolemia", "LDL cholesterol (mmol/L)", \n"Lipid lowering medication use", "Chronic kidney disease (mL/min/1.73m\xc2\xb2)", \n"=90 (reference)", "60-89", "=60"), class = c("ordered", "factor"\n)), estimate = c(518.38, 0.98, 1.07), conf_low = c(178.74, 0.93, \n0.96), conf_high = c(1503.36, 1.03, 1.19), label = c("518.38 (178.74-1503.36)", \n"  0.98 (  0.93-   1.03)", "  1.07 (  0.96-   1.19)")), row.names = c(NA, \n-3L), class = c("tbl_df", "tbl", "data.frame"))\n\nmydata\n\n# A tibble: 3 x 4\n  term        estimate conf_low conf_high\n  <ord>          <dbl>    <dbl>     <dbl>\n1 Intercept     518.     179.     1503.  \n2 Age (years)     0.98     0.93      1.03\n3 Sex (male)      1.07     0.96      1.19\n
Run Code Online (Sandbox Code Playgroud)\n

为了制作包含估计值和 95%CI 的标签,我使用了paste0,并确保每个数字都有两位小数format。然而,当组合这些时,会出现额外的空格:

\n
mydata <- \n  mydata %>% \n  mutate(\n    label=\n      paste0(format(round(estimate, digits=2), nsmall=2), \n             " (", \n             format(round(conf_low, digits=2), nsmall=2), \n             "-", \n             format(round(conf_high, digits=2), nsmall=2), \n             ")", \n             sep="", collaps=""))\n\nmydata\n# A tibble: 3 x 5\n  term        estimate conf_low conf_high label                    \n  <ord>          <dbl>    <dbl>     <dbl> <chr>                    \n1 Intercept     518.     179.     1503.   "518.38 (178.74-1503.36)"\n2 Age (years)     0.98     0.93      1.03 "  0.98 (  0.93-   1.03)"\n3 Sex (male)      1.07     0.96      1.19 "  1.07 (  0.96-   1.19)"\n
Run Code Online (Sandbox Code Playgroud)\n

为什么会出现这种情况?我可以阻止这种情况或以其他方式删除空格以使格式变为“估计(conf_low-conf_high)”吗?

\n

Dav*_*ong 5

trim=TRUE在通话中添加format()

\n
mydata %>% \nmutate(\n  label=\n    paste0(format(round(estimate, digits=2), nsmall=2, trim=TRUE), \n           " (", \n           format(round(conf_low, digits=2), nsmall=2, trim=TRUE), \n           "-", \n           format(round(conf_high, digits=2), nsmall=2, trim=TRUE), \n           ")", \n           sep="", collaps=""))\n  \n# A tibble: 3 \xc3\x97 5\n  term        estimate conf_low conf_high label                    \n  <ord>          <dbl>    <dbl>     <dbl> <chr>                    \n1 Intercept     518.     179.     1503.   "518.38 (178.74-1503.36)"\n2 Age (years)     0.98     0.93      1.03 "0.98 (0.93-1.03)"     \n3 Sex (male)      1.07     0.96      1.19 "1.07 (0.96-1.19)"     \n
Run Code Online (Sandbox Code Playgroud)\n