Yanping Chen raised a question in the Chinese COS forum on the output of Eviews: how to (re)format the decimal coefficients in equations as text output? For example, we want to round the numbers in CC = 16.5547557654 + 0.0173022117998*PP + 0.216234040485 * PP(-1) + 0.810182697599 * (WP + WG) to the 3rd decimal places. This can be simply done by regular expressions, as decimals always begin with a period (.). The basic steps are:
- find out where are the decimals in the character string;
- format them;
- replace the original decimals with formatted values;
Given a character vector, we can format the decimals with the code below:
# x: equations; FUN: formatting function; ...: passed to FUN
coefFormat = function(x, FUN, ...) {
sapply(x, function(s) {
dig = sapply(gregexpr("\\.[0-9]+", s), function(m) {
sapply(seq(along = m), function(i) {
substr(s, m[i], m[i] + attr(m, "match.length")[i] - 1)
})
})
for (j in {
if (is.null(dim(dig))) NULL else 1:dim(dig)[1]
}) {
s = sub(dig[j, 1], substring(FUN(as.numeric(dig[j, 1]), ...),
2), s, fixed = TRUE)
}
s
})
}
I used sapply() for 3 times to avoid explicit loops but consequently the code might be difficult to read. The critical part is the regular expression \\.[0-9]+ which means one of more (controlled by + after [0-9]) digits ([0-9] or [:digit:]) after a decimal point .. As . is a metacharacter in regular expressions, we need to use a backslash before it, and again, \ is a special character in R, so we need another backslash to denote a backslash. o:-)
x = readLines(zz <- textConnection(
"CC = 16.5547557654 + 0.0173022117998 * PP + 0.216234040485 * PP(-1) + 0.810182697599 * (WP + WG)
II = 20.2782089394 + 0.150221823899 * PP + 0.61594357734 * PP(-1) - 0.157787636546 * KK
WP = 1.50029688603 + C(10) * XX + 0.146673821502 * XX(-1) + 0.130395687204 * AA
"))
close(zz)
writeLines(coefFormat(x, round, digits = 3))
# CC = 16.555 + 0.017 * PP + 0.216 * PP(-1) + 0.81 * (WP + WG)
#
# II = 20.278 + 0.15 * PP + 0.616 * PP(-1) - 0.158 * KK
#
# WP = 1.5 + C(10) * XX + 0.147 * XX(-1) + 0.13 * AA
#
writeLines(coefFormat(x, formatC, digits = 3, format = "f"))
# CC = 16.555 + 0.017 * PP + 0.216 * PP(-1) + 0.810 * (WP + WG)
#
# II = 20.278 + 0.150 * PP + 0.616 * PP(-1) - 0.158 * KK
#
# WP = 1.500 + C(10) * XX + 0.147 * XX(-1) + 0.130 * AA
#
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).