Transcript Data, graphics, and programming in R
Data, graphics, and programming in R
28.1, 30.1, 2-4.2
Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen
• Wed – Basics of R (AM & PM) • Fri – Datasets in R (AM), graphics (PM) • Mon – Statistical tests and linear models (AM), more graphics (PM) • Tue – Basics of programming (AM), generalized linear models (PM) • Wed – Mixed models (AM), working with own data (PM) Teaching based on short lectures, demos, and exercises
• Lectures, demo codes, exercises, and other material can be found by following the course link in www.mv.helsinki.fi/home/akuparin/R_course.htm
• Other R material (e.g. downloading R) http://www.r-project.org/
How to get most out of the course?
• Do notes –also comment your own solution codes for the exercises • Be active –ask questions, ask clarification, ask to slow down • In the evenings, re-read the materials of the day • Start to use R immediately after the course
What is ?
• R is a software that provides a very handy environment and a large collection of tools for – Exploring and editing data – Computations, calculations, numerical mathematics – Statistical analyses – Producing graphics – Programming • R is a freeware program • R is developed by its users, i.e. anyone can write their own R extensions and share them with other users through R web – New methods are quickly implemented to R – There is a large variety of tools available
R work space Notepad or R script
Write code first here!
Then copy-paste to R.
How to use R?
• R is a text-based interface, i.e. commands are given by text lines – > one should remember at least most common text commands • In practice, commands are typically written in an editor program and them copy-pasted to R – Notepad, R script, Tinn-R… – Commenting the code is highly recommended!
• R is based on objects and functions – Objects can be variables, lists, data frames… – Functions are made to carry out specific operations – Functions are stored in “library packages” • R stores objects and commands on workspace – Workspace and command history can be saved and loaded later on – Large objects reserve a lot of memory in the workspace
• Write your command on a command line > 2+3 • Then press enter  5 • R returns the value
Placing a value to a variable
• Write your command on a command line > a=2+3 • Then press enter > • R does not return the value, but it has saved it to variable a
Operators in R
= places a value (<- would also work) == checks identity of values on the left and on the right side An command A=4/9 sets a value 4/9 to a variable called A. An command A==4/9 checks if A equals to 4/9 or not. The command returns a logical value TRUE or FALSE >= larger or of equal size <= smaller or of equal size DEMO 1
Length of this vector is 4.
1st element 2.3
Vectors in R
• Generic way of creating a vector > a=c(2.3,6.8,1.1,4.9) • Pointing to a vector element > a  2.3
• Or several elements > a[c(1,3)]  2.3 1.1
• Any calculations such as + or exp() can be applied to vectors.
A matrix element is pointed by its row and column number: A[2,4]=-2.2
5 7 1 8 3.3
0 1 9 14 5.4
Size of this matrix is 4 X 4
Matrix in R
• Generic way of creating a matrix > a=matrix(c(2,5,6,1),nrow=2,ncol=2,byrow=T) > a [,1] [,2] [1,] 2 5 [2,] 6 1 > a[1,]  2 5 > a[,2]  5 1
Tools to create vectors and matrixes
• Tools to create vectors A = c(1.0,5.8,9.0) A = 1:6 A = rep(1,9) A = rep(c(1,2),4) A = rep(c(1,2),each=4) A = seq(1,10,by=0.2) A = seq(1,10,length.out=57) • Creating matrixes B = matrix(1,nrow=8,ncol=9) B = matrix(0,nrow=9,ncol=4) -> DEMO 2
R as a calculator
• Basic math operators in R: +, -, /, *, ^ • Matrix calculations: – transpose matrixes by t() – Matrix calculations specified with %: for example * multiplies by elements, and %*% is for multiplication of matrixes • Basic mathematical functions: – log, exp, sqrt, cos, sin, tan, abs
• Rounding: round, floor, ceiling
Anatomy of an R function
OBJECT RETURNED BY THE FUNCTION NAME OF THE FUNCTION PARAMETER VALUES AND SETTINGS: not all have to be defined (see help files for the defaults)
Help in R
• Many ways to find help: – In R directly: ?
”name of the function” or help.search
(“keyword”) e.g. ?rep or help.search
(“vector”) – From R web pages – For more advanced problems also from R discussion groups – > DEMO 3