For loops

Use for loops to do something with every value in a vector/list or to do something a certain number of times

for (identifier in vector/list){
  do something
}

for (identifier in 1:somenumber){
  do something
}

If else statements

Use if else statements to only do something if one criteria (or more) is fulfilled

if (value1 == value2){
  do this
}
else if (value1 < value3){
  do this
}
else {
  do something else
}

Connecting to SQL Databases

library(obdc)
con <- dbConnect(odbc(), Driver = "SQL Server", Server = e.g "PSADS004", Database = e.g. "TrailBlazer", trusted_connection = TRUE)

Querying SQL Databases

Note: you must connect to a database first

result <- dbGetQuery(conn = con, statement = "SELECT * FROM table1...")

Summary statistics

mean(df$column) #Returns the mean
median(df$column) #Returns the median
sd(df$column) #Returns the standard deviation
quantile(df$column) #Returns the 0, 25th, 50th, 100th quantiles
summary(df) #Returns summary stats for all
#columns in df
library(psych)
describeBy(df, group) #Returns statistics by group

The group argument should be a factor


Functions

Use functions to repeat common actions and to help compartmentalise your code

function_name <- function(argument_1, argument_2, ...){
  do something
  return(return_value)
}