220 likes | 338 Views
Introduction to . GTECH 201 Session 13. What is R?. Statistics package A GNU project based on the S language Statistical environment Graphics package Programming language . Getting Started. Starting R. Getting Help. Getting help
E N D
Introduction to GTECH 201 Session 13
What is R? • Statistics package • A GNU project based on the S language • Statistical environment • Graphics package • Programming language
Getting Started • Starting R
Getting Help • Getting help > help ( ) provides help on how to use ‘help’ > help (topic) provides help on a specific topic > help.start ( ) brings you to a web interface to the R documentation
R Functions R functions take arguments (information that you put into the function which goes between the brackets) and can perform a range of tasks. In the case of the ‘help’ function the task is to display information from the R documentation files. • help ( ) is an R function
R as Calculator • R will evaluate basic calculations which you type into the console (input window)
Assigning Values • With the <- operator • With a regular = equal sign
R as Calculator • In the previous example x and y are variables. We obtained the sum of x and y by typing • x + y • In the same way we could carry out much more complicated calculations • Generally you can obtain the number (or other value) stored in any letter by typing the letter followed by enter (or by typing print (letter) or show (letter))
Simple Operations • Add 10 + 20 • Multiply 10 * 20 • Divide 10 / 20 • Raise to a power 10 ** 20 • Modulo 10 %/% 20 • Integer division 10 %% 4
Vectors • In R you can think of vectors as being equivalent to a single column of numbers. • You can create a vector using the c( ) function as follows: • x <- c( ) • e.g. x <- c(1,2,4,8) creates a column of the numbers 1,2,4,8
Simple Operations on Vectors • When you carry out simple operations (+ - * /) on vectors in R that have the same number of entries R just performs the normal operations on the numbers in the vector entry by entry • If the vectors don’t have the same number of entries then R will cycle through the vector with the smaller number of entries • Vectors can be assigned by putting together other vectors
Matrices and Lists • Matrix • Rectangular table of data of the same type • Arrays are 3-, 4-, .. n-dimensional matrices • List • An ordered collection of data of arbitrary types • > doe = list(name="john",age=28,married=F)
Data Frames • The tables we know from Excel • Each column has the same type • But different columns may be of different type
Subsetting • Individual elements of a vector, matrix, array or data frame are accessed with “[ ]” by specifying their index, or their name
Storing Data • Every R object can be stored into and restored from a file with the commands “save” and “load” • > save(x, file=“x.Rdata”) • > load(“x.Rdata”)
R Import and Export • Most programs (e.g. Excel) know how to deal with rectangular tables in the form of tab-delimited text files • > x = read.delim(“filename.txt”) • also: read.table, read.csv • > write.table(x, file=“x.txt”, sep=“\t”)
Importing Data Caveats • Type conversions • The read functions try to guess and autoconvert the data types of the different columns (e.g. number, factor, character) • Special characters • Delimiter character (space, comma, tabulator) cannot be part of a data field • To circumvent this, text may be “quoted”
Getting Help (Again) • Html search engine