1. Variable names should be short but descriptive and use a ‘.’ between words

  raw.data <- csv('...')
  player.names <- c('Jill', 'Lisa', 'Bob')
  info <- csv('...')
  important_data <- c('Jill', 'Lisa', 'Bob')

2. Function names should be short but descriptive. Every word should begin with a capital.

  AvgCount <- function(){
    ...
  }
  avg.count <- function(){
    ...
  }
  
  calculate <- function(){
    ...
  }

3. No line should be more than 80 characters.

ggplot(birthData, aes(x = births)) + 
  geom_histogram(aes(y = ..density.., fill = ..count..), bins = 25) + 
  scale_fill_gradient('Count', low = '#DCDCDC', high = '#7C7C7C') + 
  stat_function(fun         = dnorm, color='red', 
                args        = list(mean = mean(birthData$births), 
                                     sd = sd(birthData$births)), 
                show.legend = FALSE,
                aes(size = 2)) + 
  theme(legend.position = 'none') +
  scale_y_continuous('Proportion', labels = scales::comma) + 
  scale_x_continuous('Births',     labels = scales::comma) + 
  labs(title   = 'Frequency of Births in Single Day (1969-1988)', 
       caption = 'Data From: http://vincentarelbundock.github.io/Rdatasets/') 
  ggplot(birthData, aes(x=births)) + geom_histogram(aes(y=..density.., fill=..count..), bins=25) + scale_fill_gradient('Count', low='#DCDCDC', high='#7C7C7C') + 
   stat_function(fun=dnorm, color='red', args=list(mean=mean(birthData$births), sd=sd(birthData$births)), aes(size=2), show.legend=FALSE) +           
   theme(legend.position='none') +
   scale_y_continuous('Proportion', labels=scales::comma) + scale_x_continuous('Births', labels=scales::comma) + 
   labs(title='Frequency of Births in Single Day (1969-1988)', caption='Data From: http://vincentarelbundock.github.io/Rdatasets/') 

4. Place a space before and after all operations.

  average.score <- mean(players.info[, 2])
  total.goals <- sum(players.info[3, ])

5. { should be on the same lin as conditionals

  if(condition){
    ...
    ...
  }else{
    ...
    ...
  }

6. The Golden Rule

  • Always adapt to the style of the program you are writing, even if it contradicts these rules.
  • Exception: If the code you are adapting will become your own, modify it to meet your style
  • Exception: There are always exceptions.
  • Above all else, be consistent