Outline

In Session II of this R workshop, we plan to cover the following:

  1. Basic operations (addition, subtraction, multiplication, division) on single values and on vectors
  2. Indexing into vectors and into matrices/dataframe
  3. Importing a dataset from a text file, csv, xls, clipboard, … and having a quick look at it
  4. Loops and statements (for, if/else, while): how and when using them

To return to the list of sessions at the rpubs website, click here

http://rpubs.com/trialewisgroup/.

List of functions

  sqrt() # square root
  exp() # exponential function
  abs() # absolute value
  log() # natural logarithms
  min() # minimum value
  max() # maximum value
  mean() # mean value
  sd() # standard deviation
  median() # sample median
  quantile() # sample quantiles
  var() # variance
  sum() # sum
  length() # get or set the length of vectors (including lists) and factors
  read.table() # reads a text file in table format and creates a data frame from it
  read.csv() # reads a csv file in table format and creates a data frame from it
  read.xls() # reads a Microsoft Excel file into a data frame
  readClipboard() # reads the clipboard into a data frame (only available on Windows)
  read.table(file="clipboard", sep="\t", header=TRUE) # reads the clipboard into a data frame
  head() # returns the first part of a vector, matrix, table, data frame or function
  tail() # returns the last part of a vector, matrix, table, data frame or function
  summary() # returns summaries of various object classes
  dim() # retrieve or set the dimension of an object
  for(condition){instructions} # executes instructions for a certain number of times
  # can also be written:
  for(condition){
    instruction 1
    instruction 2
  }
  if(condition){instructions}else{instructions} # checks whether condition is true and executes instructions
  # can also be written:
  if(condition){
    instruction 1
    instruction 2
  }else{ # else needs to be on the same line as } and {
    instruction 1
    instruction 2
  }
  while(condition){instructions} # executes instructions while condition is true
  # can also be written:
  while(condition){
    instruction 1
    instruction 2
  }

Best practices: preparing a dataset for import in R

Adapted from https://www.datacamp.com/community/tutorials/r-tutorial-read-excel-into-r

  • The first row of the spreadsheet is usually reserved for the header, while the first column is used to identify the sampling unit (if necessary).
  • Use only one cell for header names in opposition to writing the rest of a long name on the second row.
  • In the same way, use only one row per individual.
  • Do not use the second row for unit (see first two points).
  • Avoid names/values/fields with blank spaces, otherwise each word will be interpreted as a separate variable, resulting in errors that are related to the number of elements per line in your data set. If you want to concatenate words, do this by inserting a dot (e.g. Sepal.Length).
  • Avoid any blank row or column.
  • Short names are prefered over longer names.
  • Try to avoid using names that contain symbols such as: ? $ % ^ & * ( ) - # , < > / | \ [ ] { } ` ’
  • Delete any comments that you have made in your Excel file to avoid extra columns or NA’s to be added to your file.
  • Make sure that any missing values in your data set are indicated with NA.