李家翔 2019-01-23
参考irudnyts
# Good
add()
# Bad
addition()
# Good
if (is_used) {
# do something
}
if (is_used) {
# do something
} else {
# do something else
}
# Bad
if (is_used)
{
# do something
}
if (is_used) { # do something }
else { # do something else }
There are two common number of spaces for indentation: two (Hadley and others) and four (Bioconductor). My own rule of thumb: I use four spaces indentation for data analyses scripts, and two spaces while developing packages.
在做数据分析的时候,建议用4个空格的缩进,保持 R 和 Python 的统一。
# Good
normal_pdf <- 1 / sqrt(2 * pi * d_sigma ^ 2) *
exp(-(x - d_mean) ^ 2 / 2 / s ^ 2)
# Bad
normal_pdf <- 1 / sqrt(2 * pi * d_sigma ^ 2)
* exp(-(x - d_mean) ^ 2 / 2 / d_sigma ^ 2)
R 对于 Bad 的例子中,* exp(-(x - d_mean) ^ 2 / 2 / d_sigma ^ 2)
没有识别。