Presentation on theme: " Statistics package Graphics package Programming language Can be used to share/reproduce analyses Many new packages being created - can be downloaded."— Presentation transcript:
Statistics package Graphics package Programming language Can be used to share/reproduce analyses Many new packages being created - can be downloaded and easily installed Largely text-based interface Introduction to the R language What is R?
Statistical and numerical methods High quality visualization and graphics tools Effective, extensible user interface Compiles and runs on UNIX, Windows and Macs Open source i.e., freely software Strengths of R
Software, documentation, manuals and support: More information about RR The Comprehensive R Archive NetworkComprehensive R Archive Network The 'official' introduction to R'official' introduction to R R software
R has several packages available R has more than 1000 functions and commands It is impossible to remember them all Each function may also take several arguments One needs to use the help system and manuals regularly Working with R
Once R is installed on your system, click on the R icon Upon opening, you will see the following console window: Getting started
The R program issues a prompt ‘>’ when it expects input commands E.g., type in 2+2 and press the enter key You will see: Prompt and Command line > 2+2  4 > The result is 4. The  says “first requested element will follow”. Here, there is just one element. The prompt returns waiting for further input. The result is 4. The  says “first requested element will follow”. Here, there is just one element. The prompt returns waiting for further input.
For each “project” on which one works one should use a different directory (=folder) At the end of each session, R asks whether one wants to save the workspace image - all objects are saved On double clicking on a workspace image file, R starts and loads the workspace - all objects are restored When the workspace image is saved, the history of recently typed commands is saved Some peculiarities of R
The workspace stores all commands Previous commands can be retrieved using the up/down arrows R is case sensitive Using UNIX platforms, UNIX commands can be used: Elementary R mkdir test -> make directory called test cd test-> change to test directory mkdir test -> make directory called test cd test-> change to test directory Using windows, use the File menu to open and save files, or change directories
Excel files should first be saved as tab-delimited text files, before they can be read into R In R, the full pathname of files should be specified – go to the File menu, change directories to specify the location of the file to be opened Open files with filenames and their extentions See R Data Import/Export manuals in the Help menu Importing Data
So, to view a tab-delimited file, stored on your desktop: 1.Set directory in the File menu 2.At the prompt, type:read.delim(“filename.txt") 3.The data will now be shown on the R console 4.Naming the datafile: x=read.delim(“filename.txt") Importing Data - example Various ways of reading in the files > x = read.delim(“filename.txt”) > x <- read.table (“filename.txt”) > x <- read.csv (“filename.txt”)
The exit or quit command is > q() OR click on the File menu and then on Exit OR click on the X in the top right hand corner of the R window Remember, there will be a message asking whether to save the workspace image. Clicking Yes (the safe option) will save all the objects that remain in the workspace – any that were there at the start of the session and any that have been added since. Saving and Quitting
You may have the same variable names within two different datasets, and so it’s necessary to attach a dataset: attach(filename) When you ask R to use a value of a variable it needs to find, R searches through several ``environments'' for these variables By attaching a data frame, you put the names into the second environment searched (the name of the dataframe is in the first). These are masked by any variables which already have the same name Attaching files
Important things to remember: You can change the values of the variables in the data frame, but you must remember to reference the data frame Variables removed from unattached files, will not be visible when listing variables (where ls() lists all known variables), but those variables in fact still there You must detach the dataset between uses of these variables, or you may forget which variable you are referring to: detach(filename) Attaching and detaching files
Getting Help > help ( ) provides help on how to use ‘help’ > help (topic)provides help on a specific topic > help.start ( )opens R documentation on the internet > help ( ) provides help on how to use ‘help’ > help (topic)provides help on a specific topic > help.start ( )opens R documentation on the internet Various options are available on the help function Also, use the help menu This is incredibly useful – get to know it and use it!
R as a calculator R will evaluate basic calculations which are typed into the console (input window) Try a few examples ….
Value Assignments Values can be assigned using the <- operator, which consists of the two characters ‘<’ (“less than”) and ‘-’ (“minus”) occurring side-by-side, and it ‘points’ to the object receiving the value of the expression. Example: > x <- 10 > y <- 20 > x + y  30 > variables
Value Assignments More complicated calculations To obtain the number (or other value) stored in any letter: - type the letter followed by enter - type print (letter) - type show (letter)
Simple operations Try some of these simple operations: Add: Multiply: 10 * 20 Divide: 10/20 Raise to a power: 10 ** 20