Imperial College/Courses/Fall2009/Synthetic Biology (MRes class)/'R' Tutorial/Crash Course
From OpenWetWare
Introduction to 'R'
- Overview
- Crash Course
- Basic Commands
- Practical
Slides
Useful online resources
- R Tutorial from CRAN, other Basic Tutorial
- R by example
- R Graphics Tutorial, R GRaph Gallery.
Crash Course in R
Running 'R'
- > Starting R
- Find and Select 'R' within your Programs list
- > Running 'R' from command line
- Once started, 'R' will open an interactive console.
- You can then type 'R' commands. Remember, 'R' commands are case sensitive.
- Commands are separated either by a semi-colon (‘;’), or by a newline.
- The vertical arrow keys on the keyboard can be used to scroll forward and backward through a command history.
- > Running 'R' From a script (strongly suggested)
- 'R' can runs commands that have been saved in a text file (you can use your prefered text editor)
- Commands are separated either by a semi-colon (‘;’), or by a newline.
- For good practice, comments should be added to the script by using '#', like '# this is my comment'
- to execute the script, type in the 'R' console source("path_to_my_script/my_script.R")
Exit R
- type q() in 'R' console
Getting help
- Check online tutorials (see above)
- Type help(command_name) or ?command_name like help(length), ?length
Objects in R
- > Data Types available
- integer
- numeric
- character
- complex
- logical
- > Data Structures available
- Vectors
- A vector is an ordered collection of elements and is equivalent to a single row or a single column in a spreadsheet. Most vectors are numerical (1,2,3…), but logical vectors (TRUE, FALSE, TRUE…) and vectors of character strings (“apples”, “oranges”, “pears”…) also are possible.
- Matrices and Arrays
- A matrix is a set of vectors extending over two dimensions; an array is a set of vectors extending over two or more dimensions. A matrix, therefore, is a two-dimensional array.
- Factors
- A factor is a vector object used to specify a discrete classification (grouping) of the components of other vectors of the same length. R provides both ordered and unordered factors.
- Lists
- A list is a vector containing different types of elements, such as (1, TRUE, “apples”…)
- Data Frames
- A data frame is a matrix-like object consisting of two or more lists or vectors (or matricies or data frames), each of which contains the same number of elements. The objects in a data frame need not be of the same type.
- Vectors
- > Listing objects in memory
- type objects() in 'R' console.
- > Clearing memory
- type rm(object_name)
- to clear all memory type rm(objects())
Import / Export Data
Graphics in 'R'
Useful functions
- Generating regular expressions
- seq()
- rep()
- 1:30
- ....
- Generating regular expressions
- Write your own functions