introduction to gtech 201 session 13. what is r? statistics package a gnu project based on the s...
Post on 18-Dec-2015
215 views
TRANSCRIPT
Introduction to
GTECH 201Session 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 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.
R Functions
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
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
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
Simple Operations on Vectors
Combining 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