Introduction to R Language

This tutorial presents an introduction to R programming.

Table of Contents

1  Preliminaries

  • R language is case sensitive
  • R names consist of alphanumeric symbols, plus ‘.’ and ‘_’, with a restriction that a name must start with ‘.’ or a letter.
  • Two kinds of basic R commands: (1) expressions (evalue, then print, then value is lost); (2) assignment (evaluate, store the value, but not print).
  • Multiple commands can co-exist in one line, separated by ‘;’. Nultiple commands can also be grouped together into one compound expression by a pair of braces ‘{‘ abd ‘}’.
  • We can run a R file, say example.R, by the command
    > source(“example.R”)
  • By default, R outputs evaluation results to the console. However, the outputs can be re-directed to a file, say output.txt, by the command
    > sink(“output.txt”)
    and such redirection can be stopped to resume normal console output by
    > sink()
  • To get the help of a function, for example, solve(), we can use the commands
    >help(solve)  or
    > ?solve
  • The command
    > objects()
    returns the names of objects in the workspace under current R session, and the rm() function can be used to remove objects from the workspace
    > rm(x,y,z, temp,foo)
  • When exiting a R session, R prompts to ask whether to save the workspace, meaning that all objects will be saved to a .RData file, and all command lines will be saved to a .Rhistory file. Later if R is started from same directory, these history data will be loaded into R session. It is recommended that you should use separate working directory for analyses conducted with R. 

Leave a Reply

Your email address will not be published. Required fields are marked *