180 likes | 383 Views
Introduction to the R language. What is R?. 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. Strengths of R.
E N D
Introduction to the R language What is R? • 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
Strengths of 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
R software • Software, documentation, manuals and support: • More information about R http:// www.r-project.org/ • The Comprehensive R Archive Network http://cran.r-project.org/ • The 'official' introduction to R http://cran.r-project.org/doc/manuals/R-intro.pdf
Working with R • 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
Getting started • Once R is installed on your system, click on the R icon • Upon opening, you will see the following console window:
Prompt and Command line • 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: > 2+2 [1] 4 > The result is 4. The [1] says “first requested element will follow”. Here, there is just one element. The prompt returns waiting for further input.
Some peculiarities of R • 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
Elementary 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: 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
Importing Data • 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
> x = read.delim(“filename.txt”) > x <- read.table (“filename.txt”) > x <- read.csv (“filename.txt”) Various ways of reading in the files Importing Data - example So, to view a tab-delimited file, stored on your desktop: Set directory in the File menu At the prompt, type: read.delim(“filename.txt") The data will now be shown on the R console Naming the datafile: x=read.delim(“filename.txt")
Saving and Quitting • 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.
Attaching files • 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 and detaching 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)
Getting Help • Various options are available on the help function > help ( ) provides help on how to use ‘help’ > help (topic) provides help on a specific topic > help.start ( ) opens R documentation on the internet • 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 ….
variables 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 [1] 30 >
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: 10 + 20 • Multiply: 10 * 20 • Divide: 10/20 • Raise to a power: 10 ** 20