GOOD: tabPrior <- table(df[df$daysFromOpt < 0, "campaignid"]) total <- sum(x[, 1]) total <- sum(x[1, ])
BAD:
tabPrior <- table(df[df$daysFromOpt<0, "campaignid"]) # Needs spaces around '<' tabPrior <- table(df[df$daysFromOpt < 0,"campaignid"]) # Needs a space after the comma tabPrior<- table(df[df$daysFromOpt < 0, "campaignid"]) # Needs a space before <- tabPrior<-table(df[df$daysFromOpt < 0, "campaignid"]) # Needs spaces around <- total <- sum(x[,1]) # Needs a space after the comma total <- sum(x[ ,1]) # Needs a space after the comma, not before
左括号前留空格 函数调用时例外
GOOD: if (debug) BAD: if(debug)
如果有助于对齐可以使用多个空格 plot(x = xCoord, y = dataMat[, makeColName(metric, ptiles[1], "roiOpt")], ylim = ylim, xlab = "dates", ylab = metric, main = (paste(metric, " for 3 samples ", sep="")))
不要在括号或者方括号中代码前后加空格
GOOD: if (debug) x[1, ] BAD: if ( debug ) # No spaces around debug x[1,] # Needs a space after the comma
# Create histogram of frequency of campaigns by pct budget spent. hist(df$pctSpent, breaks = "scott", # method for choosing number of buckets main = "Histogram: fraction budget spent by campaignid", xlab = "Fraction of budget spent", ylab = "Frequency (count of campaignids)")
CalculateSampleCovariance <- function(x, y, verbose = TRUE) { # Computes the sample covariance between two vectors. # # Args: # x: One of two vectors whose sample covariance is to be calculated. # y: The other vector. x and y must have the same length, greater than one, # with no missing values. # verbose: If TRUE, prints sample covariance; if not, not. Default is TRUE. # # Returns: # The sample covariance between x and y. n <- length(x) # Error handling if (n <= 1 || n != length(y)) { stop("Arguments x and y have invalid lengths: ", length(x), " and ", length(y), ".") } if (TRUE %in% is.na(x) || TRUE %in% is.na(y)) { stop(" Arguments x and y must not have missing values.") } covariance <- var(x, y) if (verbose) cat("Covariance = ", round(covariance, 4), ".\n", sep = "") return(covariance) }
待办模版
在代码中使用统一的模版
TODO(username): Explicit description of action to be taken
http://www.maths.lth.se/help/R/RCC/ - R Coding Conventions http://ess.r-project.org/ - For emacs users. This runs R in your emacs and has an emacs mode. === update 1 没想到科学网的编辑器如此难用 直接从evernote里copy带格式的文本反而乱码一片